Because the file should be generated dynamically, maybe I should use the fs modules's writeStream. But I couldn't find any example codes with my poor googling. Sorry.
More specifically, I want to give a CSV file or a PDF file with my datas in the MongoDB, when someone requests.
Anyone, please give me some hints.
Thanks.
Downloading a file using node js can be done using inbuilt packages or with third party libraries. GET method is used on HTTPS to fetch the file which is to be downloaded. createWriteStream() is a method that is used to create a writable stream and receives only one argument, the location where the file is to be saved.
var path = require('path'); var mime = require('mime'); var fs = require('fs'); app. get('/download', function(req, res){ var file = __dirname + '/upload-folder/dramaticpenguin. MOV'; var filename = path. basename(file); var mimetype = mime.
With the express, I can implement like this.
app.get('/down2', function(req, res){
var filename = 'data.csv';
res.attachment(filename);
res.end('hello,world\nkeesun,hi', 'UTF-8'); //Actually, the data will be loaded form db.
});
How simple is it. Thanks.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With