admin管理员组文章数量:1334333
The following callback function sends an empty file to the browser even though the file contains 'helloworld' on the server:
router.get('/Download', function(req, res) {
var fs = require('fs')
fs.writeFile('helloworld.txt', 'helloworld');
res.download('helloworld.txt');
})
The following callback function sends an empty file to the browser even though the file contains 'helloworld' on the server:
router.get('/Download', function(req, res) {
var fs = require('fs')
fs.writeFile('helloworld.txt', 'helloworld');
res.download('helloworld.txt');
})
Share
Improve this question
asked Dec 10, 2015 at 12:52
SANBI samplesSANBI samples
2,1182 gold badges16 silver badges20 bronze badges
1
- Programming with Node.JS is asynchronous programming. You should get use with that, if you want to get the maximum of Node.js. – Rolice Commented Dec 10, 2015 at 13:02
2 Answers
Reset to default 7writeFile
is asynchronous.
either use it as:
fs.writeFile('helloworld.txt', 'helloworld', function () {
res.download('helloworld.txt');
});
or use writeFileSync
https://nodejs/api/fs.html#fs_fs_writefile_file_data_options_callback
Try to find out if your code has
process.exit()
for some reason. If you do, ment out this one and you will be good to go. My version is v8.6.0.
See also: node - fs.writeFile creates a blank file
本文标签:
版权声明:本文标题:javascript - Why is Node fs.writeFile() method successful, but an empty file is then sent to the browser? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742368624a2461778.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论