javascript 在Express JS中完成www.example.com后的更好方法res.download?

afdcj2ne  于 2022-12-02  发布在  Java
关注(0)|答案(1)|浏览(117)

I have an express get route that downloads a CSV file in the browser. 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.
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:

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. There has got to be a better way to run this fs.unlink block of code only once the res.download finishes. I am new to express js so I don't know how to do this. 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

dwbf0jvd

dwbf0jvd1#

根据Express文档,res.download()可以接受回调函数,当传输完成或发生错误时调用该函数:

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

以下是Express文档中相关部分的链接。
希望这对你有帮助。

相关问题