简体   繁体   English

在 Express JS 中 res.download 之后做某事的更好方法?

[英]Better way to do something after res.download in Express JS?

I have an express get route that downloads a CSV file in the browser.我有一个快速获取路线,可以在浏览器中下载 CSV 文件。 That part works fine, but now I want to delete the file using fs.unlink after it is downloaded because it is being stored on the host os in the temporary directory and they build up.那部分工作正常,但现在我想在下载文件后使用fs.unlink删除文件,因为它存储在临时目录中的主机操作系统上,并且它们会累积。

If I try to run the code snipped below, it cannot complete the download because it gets deleted by unlink before it can process (I think).如果我尝试运行下面截断的代码,它无法完成下载,因为它在处理之前被取消链接删除(我认为)。

router.get('/downloadCsv', function(req, res, next) {
  res.download(downloadCsvPath);//download file
  fs.unlink(downloadCsvPath, (err) => {//delete file from server os
    if (err) {
      console.error(err);
    } else {
      console.log('removed: ' + downloadCsvPath);
    }
  });
});

It gives this error:它给出了这个错误:

Error: ENOENT: no such file or directory, stat '/var/folders/fw/03jxpm311vs548bvf9g_1vxm0000gq/T/<filename>.csv'

As a workaround, I added a 3 second timeout to let the file download first, then delete:作为解决方法,我添加了 3 秒超时让文件先下载,然后删除:

router.get('/downloadCsv', function(req, res, next) {
  res.download(downloadCsvPath);//download file
  setTimeout(() => {
    fs.unlink(downloadCsvPath, (err) => {
      if (err) {
        console.error(err);
      } else {
        console.log(downloadCsvPath);
      }
    });
  }, 3000);
});

This works, I get the file downloaded and then it gets deleted from the temporary directory.这有效,我下载了文件,然后从临时目录中删除了它。

But, if connection was poor and it takes longer than 3 seconds, it would probably fail again.但是,如果连接不佳并且花费的时间超过 3 秒,它可能会再次失败。 There has got to be a better way to run this fs.unlink block of code only once the res.download finishes.必须有更好的方法来仅在 res.download 完成后运行此 fs.unlink 代码块。 I am new to express js so I don't know how to do this.我是表达 js 的新手,所以我不知道该怎么做。 I want to do something like:我想做类似的事情:

res.download(path).then(() => {
    //do something
});

But it does not offer this.但它不提供这个。

I want to run a function after my expressjs res.download, but I do not know a way other than setTimeout我想在我的 expressjs res.download 之后运行 function,但我不知道除了 setTimeout 之外的其他方法

As per Express documentation, res.download() can accept a callback function, which is invoked when the transfer is complete or when an error occurs:根据 Express 文档, res.download()可以接受回调 function,在传输完成或发生错误时调用该回调:

router.get('/downloadCsv', (req, res, next) => {
  res.download(downloadCsvPath, (err) => {
    if (err) {
      // Handle error, but keep in mind the response may be partially-sent
      // so check res.headersSent
    } else {
      fs.unlink(downloadCsvPath, (err) => {
        if (err) {
          console.error(err);
        } else {
          console.log(downloadCsvPath);
        }
      });
    }
  });
});

Here is the link to the relevant section in the Express documentation .这是 Express 文档中相关部分的链接

Hope this helps.希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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