How to implement a file download with Node.js's fs module and express

前端 未结 2 1314
再見小時候
再見小時候 2021-02-15 16:42

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.

2条回答
  •  不要未来只要你来
    2021-02-15 16:55

    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.

提交回复
热议问题