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
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

本文标签: