angularjs - Downloading file in node.js into user specified path -


i'm trying download file using node.js , code:

var file_url = req.body.filename; var download_dir = './downloads/'; var options = {     host: url.parse(file_url).host,     port: 80,     path: url.parse(file_url).pathname }; var file_name = url.parse(file_url).pathname.split('/').pop(); var file = fs.createwritestream(download_dir + file_name);   http.get(options, function (resp) {     resp.on('data', function (data) {         file.write(data);     }).on('end', function () {         file.end();                    console.log(file_name + ' downloaded ' + download_dir);                delet(file_url);          }); });   

here i'm giving download_dir manually downloading

directory localhost not problem, when i'm uploading

code server file should downloaded particular

user's machine, how give path dynamically download_dir variable

if downloads folder in home directory (and want download in downloads folder), can use

var path = require('path');  var file_url = req.body.filename;        var download_dir = path.join(process.env.home || process.env.userprofile, 'downloads/'); var file_name = url.parse(file_url).pathname.split('/').pop(); var file_path = path.join(download_dir,file_name); var file = fs.createwritestream(file_path);  

ps: while dealing paths , files, use path module of nodejs generate path.


Comments

Popular posts from this blog

javascript - Google App Script ContentService downloadAsFile not working -

javascript - Function overwritting -

php - Find a regex to take part of Email -