简体   繁体   中英

How to download a file in the browser directly from the node.js server side without any variable?

My app is created with mean and I am a user of docker too. The purpose of my app is to create and download a CSV file. I already created my file, compressed it and placed it in a temp folder (the file will be removed after the download). This part is in the nodejs server side and works without problems.

I already use several things like (res.download) which is supposed to download directly the file in the browser but nothing append. I tried to use blob in the angularjs part but it doesn't work.

The getData function creates and compresses the file (it exists I can reach it directly when I look where the app is saved).

exports.getData = function getData(req, res, next){
  var listRequest = req.body.params.listURL;
  var stringTags = req.body.params.tagString;
  //The name of the compressed CSV file
  var nameFile = req.body.params.fileName;
  var query = url.parse(req.url, true).query;
  //The function which create the file
  ApollineData.getData(listRequest, stringTags, nameFile)
    .then(function (response){
      var filePath = '/opt/mean.js/modules/apolline/client/CSVDownload/'+response;
      const file = fs.createReadStream(filePath);
      res.download(filePath, response);
    })
    .catch(function (response){
      console.log(response);
    });
};

My main problem is to download this file directly in the browser without using any variable because it could be huge (like several GB). I want to download it and then delete it.

There is nothing wrong with res.download

Probably the reason why res.download don't work for you is b/c you are using AJAX to fetch the resource, Do a regular navigation. Or if it requires some post data and another method: create a form and submit.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM