Javascript 捕获electron中window.open()上的保存和取消事件
我正在用js本地保存一个文件,如下所示Javascript 捕获electron中window.open()上的保存和取消事件,javascript,electron,windows-10,dom-events,Javascript,Electron,Windows 10,Dom Events,我正在用js本地保存一个文件,如下所示 var win = window.open(appConfig.server_url + '/v1/discussion/files/download/chat' + '/' + params); 在浏览器上,这将打开一个新选项卡,下载文件并关闭选项卡,但在electron上,这将打开一个文件保存对话框,如下所示: 因此,我想在js中捕获save and cancel事件,这样在单击save(保存)时,用户应获得“download successful
var win = window.open(appConfig.server_url + '/v1/discussion/files/download/chat' + '/' + params);
在浏览器上,这将打开一个新选项卡,下载文件并关闭选项卡,但在electron上,这将打开一个文件保存对话框,如下所示:
因此,我想在js中捕获save and cancel事件,这样在单击save(保存)时,用户应获得“download successful”(下载成功)消息,而在cancel(取消)时,则不会发生任何事情。您可以将侦听器注册到
会话
以捕获'will-download'
事件。在回调中,您可以访问当前的下载项
,并可以对其执行任何操作
电子:
我希望这有帮助 请显示您的代码。单击“保存”按钮时,此窗口被称为“打开”,其他任何操作都不会影响流程。
// In the main process.
const {BrowserWindow} = require('electron')
let win = new BrowserWindow()
win.webContents.session.on('will-download', (event, item, webContents) => {
// Set the save path, making Electron not to prompt a save dialog.
item.setSavePath('/tmp/save.pdf')
item.on('updated', (event, state) => {
if (state === 'interrupted') {
console.log('Download is interrupted but can be resumed')
} else if (state === 'progressing') {
if (item.isPaused()) {
console.log('Download is paused')
} else {
console.log(`Received bytes: ${item.getReceivedBytes()}`)
}
}
})
item.once('done', (event, state) => {
if (state === 'completed') {
console.log('Download successfully')
} else {
console.log(`Download failed: ${state}`)
}
})
})