最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Why is Node fs.writeFile() method successful, but an empty file is then sent to the browser? - Stack Overflow

programmeradmin0浏览0评论

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
Add a ment  | 

2 Answers 2

Reset to default 7

writeFile 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

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论