I am writing a NodeJS Electron App to be distributed on all platforms. I have a download button that I would like to pop open a Save As dialog with the file being provided from
On the HTML button:
In your javascript file:
// Include in the render side
var elerem = require('electron').remote;
var dialog = elerem.dialog;
var app = elerem.app;
var http = require('http');
var fs = require('fs');
var path = require('path');
function myUrlSaveAs(remoteUrl){
// app.getPath("desktop") // User's Desktop folder
// app.getPath("documents") // User's "My Documents" folder
// app.getPath("downloads") // User's Downloads folder
var toLocalPath = path.resolve(app.getPath("desktop"), path.basename(remoteUrl)
var userChosenPath = dialog.showSaveDialog({ defaultPath: toLocalPath });
if(userChosenPath){
download (remoteUrl, userChosenPath, myUrlSaveAsComplete)
}
}
function myUrlSaveAsComplete(err){
alert("done");
}
function download (url, dest, cb) {
var file = fs.createWriteStream(dest);
var request = http.get(url, function(response) {
response.pipe(file);
file.on('finish', function() {
file.close(cb); // close() is async, call cb after close completes.
});
}).on('error', function(err) { // Handle errors
fs.unlink(dest); // Delete the file async. (But we don't check the result)
if (cb) cb(err.message);
});
};