【问题标题】:How to download a file in the browser directly from the node.js server side without any variable?如何在没有任何变量的情况下直接从 node.js 服务器端下载浏览器中的文件?
【发布时间】:2019-09-06 13:57:53
【问题描述】:

我的应用是用mean 创建的,我也是 docker 的用户。我的应用程序的目的是创建和下载 CSV 文件。我已经创建了我的文件,将其压缩并放在一个临时文件夹中(下载后该文件将被删除)。这部分在 nodejs 服务器端,可以正常工作。

我已经使用了一些东西,比如 (res.download),它应该直接在浏览器中下载文件,但没有附加任何内容。我尝试在 angularjs 部分使用 blob,但它不起作用。

getData 函数创建并压缩文件(它存在,当我查看应用程序的保存位置时可以直接访问它)。

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);
    });
};

我的主要问题是直接在浏览器中下载这个文件而不使用任何变量,因为它可能很大(比如几个 GB)。我想下载它然后删除它。

【问题讨论】:

    标签: javascript node.js angularjs mean-stack


    【解决方案1】:

    res.download没有错

    res.download 不适合您的原因可能是因为您正在使用 AJAX 获取资源,请进行常规导航。或者,如果它需要一些发布数据和另一种方法:创建一个表单并提交。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-12-18
      • 1970-01-01
      • 2011-12-19
      • 2016-05-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多