2
我正在使用node.js和express。我想在服務器上創建一個文件,然後在終點點擊時下載。 這是我現在的代碼。如何使用快遞從服務器上下載文件
router.get('/download', (req, res) => {
const fileController = new FileController();
fileController.generateJSONFile()
.then((file) => {
fs.writeFile('fooFile.json', file, 'utf8');
}).then((success) => {
res.download('fooFile.json');
})
.catch((error) => {
res.status(500).send();
});
});
我也想在下載完成後立即將文件從服務器上刪除。
如果有更好的方法來實現這個目標,我不需要使用res.download()。