Reputation: 4702
I am saving a file locally in js like this
var win = window.open(appConfig.server_url + '/v1/discussion/files/download/chat' + '/' + params);
on browser this opens a new tab and downloads the files and closes the tab, but on electron this opens a file save dialog like shown below:
So I want to capture the save and cancel event in the js so that on clicking save user should get "download successful" message and on cancel nothing should happen.
Upvotes: 0
Views: 1434
Reputation: 5531
You can register listener to session
to catch 'will-download'
event. In the callback you have access to the current DownloadItem
and can do whatever you want with it.
Electron docs example:
// 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}`)
}
})
})
I hope this helps!
Upvotes: 1