gpt4 book ai didi

node.js - 如何在 Electron 应用程序中显示另存为对话框?

转载 作者:IT老高 更新时间:2023-10-28 23:20:56 27 4
gpt4 key购买 nike

我正在编写一个要在所有平台上分发的 NodeJS Electron 应用程序。我有一个下载按钮,我想弹出一个“另存为”对话框,其中包含从服务器提供的文件。有人知道最好的方法吗?

以下是我在本地运行 Node 应用程序时尝试过的工作,但在我使用 Electron 打包程序打包应用程序后失败:

  • 将 window.location.href 设置为文件的位置
  • 将隐藏 iframe 的 src 设置为文件的位置

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

最佳答案

查看 Electron 文档 https://github.com/atom/electron/blob/master/docs/api/dialog.md 上的此页面

有一段关于dialog.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

关于node.js - 如何在 Electron 应用程序中显示另存为对话框?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32979630/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com