0

i have a server nodejs, when download a file from server

function download(info) {
    var res = info.res;
    var file = __dirname + '/static/downloads/abc.txt';

    res.writeHead(200, {
        'Content-Type': 'text/plain',
        'Content-Length': stat.size
    });

    var readStream = fs.createReadStream(file);
    readStream.pipe(res);
}

I don't understand about how can i save file to the disk in client?

daisukinahito
  • 33
  • 1
  • 7

1 Answers1

1

When you offer content type text/plain, but no content-disposition. So your browser will show it in the window...

Use:

Content-Type: application/octet-stream
Content-Disposition: attachment;filename=\"My Text File.txt\"

Look here for more details

Community
  • 1
  • 1
kumarharsh
  • 18,961
  • 8
  • 72
  • 100
  • var client = new XMLHttpRequest(); client.onreadystatechange = function() { }; client.open("GET", "/download"); client.send(); i fixed same your solution but it show text in console, no file have to save – daisukinahito Apr 09 '15 at 07:29