Save file from anchor tag using Electron
In script you can use the save file dialog by using the dialog module:
var fs = require('fs');
var dialog = require('dialog');
dialog.showSaveDialog(options, function (filePath) {
fs.writeFile(filePath, pdfContents, function (err) {
if(err) console.error(err);
});
});
Here is the documentation:
https://github.com/atom/electron/blob/master/docs/api/dialog.md#dialogshowsavedialogbrowserwindow-options-callback
What I'm doing is two-fold.
mainWindow.webContents.on('new-window', function(event, url) {
event.preventDefault();
console.log("Handing off to O/S: "+url);
shell.openExternal(url);
});
That is there so that whenever a page in my app wants to open a new window, that'll happen in an actual browser. This is also good for opening PDFs and such.
Then I just make sure that any download links use target=_blank or window.open() and the download will happen in the user's browser.