如何在电子应用程序中显示“另存为”对话框? [英] How can I display a Save As dialog in an Electron App?

查看:74
本文介绍了如何在电子应用程序中显示“另存为”对话框?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个NodeJS Electron App,可以在所有平台上分发。我有一个下载按钮,我想用服务器提供的文件弹出一个另存为对话框。有人知道做到这一点的最佳方法吗?

I am writing a NodeJS Electron App to be distributed on all platforms. I have a download button that I would like to pop open a Save As dialog with the file being provided from the server. Does anybody know the best way to do this?

这是我在本地运行节点应用程序时尝试过的方法,但是在用电子封装应用程序后却失败了-packager:

Here are the things I have tried that work when running the node app locally but fail after I have packaged the app with electron-packager:


  • 将window.location.href设置为文件的位置

  • 设置隐藏的iframe的src到文件的位置

运行打包的Mac应用时, did-fail-load事件被触发并阻止显示另存为对话框。查看网络请求时,可以看到已成功从服务器检索文件。我似乎无法弄清楚为什么会触发 did-fail-load事件。

When running the packaged mac app, the "did-fail-load" event is fired and prevents the Save As dialog from showing. When looking at the network requests, I can see that the file is successfully retrieved from the server. I can't seem to figure out why the "did-fail-load" event is being fired.

推荐答案

看看在此页面上的电子文档 https://github.com/原子/电子/斑点/母版/docs/api/dialog.md

Take a look at this page on the electron docs https://github.com/atom/electron/blob/master/docs/api/dialog.md

其中有关于 dialog.showSaveDialog

然后,您可以使用保存对话框中的URL,将其与以下功能类似,以将其保存到该位置。

Then you can use the URL from the save dialog with a function similar to the one below to save it to that location.

session.on('will-download', function(event, item, webContents) {
  event.preventDefault();
  require('request')(item.getUrl(), function(data) {
    require('fs').writeFileSync('/somewhere', data);
  });
});

在此页面上找到 https://github.com/atom/electron/blob/master/docs/api/session.md

这篇关于如何在电子应用程序中显示“另存为”对话框?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆