即使服务器上的文件包含“helloworld”,以下回调函数也会向浏览器发送一个空文件:
router.get('/Download', function(req, res) {
var fs = require('fs')
fs.writeFile('helloworld.txt', 'helloworld');
res.download('helloworld.txt');
})
最佳答案
writeFile
是异步的。
要么将其用作:
fs.writeFile('helloworld.txt', 'helloworld', function () {
res.download('helloworld.txt');
});
或使用writeFileSync
https://nodejs.org/api/fs.html#fs_fs_writefile_file_data_options_callback
关于javascript - 为什么 Node fs.writeFile() 方法成功,但随后向浏览器发送了一个空文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34202420/