我正在编写一个要在所有平台上分发的 NodeJS Electron 应用程序。我有一个下载按钮,我想弹出一个“另存为”对话框,其中包含从服务器提供的文件。有人知道最好的方法吗?
以下是我在本地运行节点应用程序时尝试过的方法,但在使用电子打包器打包应用程序后失败:
- 将 window.location.href 设置为文件的位置
- 将隐藏 iframe 的 src 设置为文件的位置
运行打包的 mac 应用程序时,会触发“did-fail-load”事件并阻止显示“另存为”对话框。当查看网络请求时,我可以看到文件已从服务器成功检索。我似乎无法弄清楚为什么会触发“did-fail-load”事件。
看看电子文档上的这个页面https://github.com/atom/electron/blob/master/docs/api/dialog.md https://github.com/atom/electron/blob/master/docs/api/dialog.md
有一节是关于对话框.showSaveDialog
然后,您可以使用保存对话框中的 URL 以及类似于下面的功能将其保存到该位置。
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 https://github.com/atom/electron/blob/master/docs/api/session.md
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)