How to implement file upload using Node.js fs module and express

Since the file should be generated dynamically, perhaps I should use the fs writeStream modules. But I could not find any code examples with my poor search. Unfortunately.

In particular, I want to provide a CSV file or a PDF file with my data in MongoDB when someone asks.

Someone please give me some advice.

Thanks.

+8
download express
source share
2 answers

With express, I can implement 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 easy it is. Thanks.

+16
source share

you don't need fs, just put your data from db into the request handler

you can set file metadata using the Content-Disposition header

 Content-Type: image/jpeg Content-Disposition: attachment; filename=genome.jpeg; modification-date="Wed, 12 Feb 1997 16:29:51 -0500"; Content-Description: a complete map of the human genome 
+2
source share

All Articles