I have a download button in HTML that when is clicked a POST request is made via ajax sending the filename of the file that must be downloaded.
On the server side I do something like this:
function download (req, res) {
...
// path is an absolute path to a file that is not in the public
// directory. I want to download that file
res.writeHead(200, {
"Content-disposition": "attachment;filename=\"" + path + "\"",
"Content-Type": "text/csv"
});
var filestream = fs.createReadStream(path);
filestream.pipe(res);
};
I can see the file content in the response but the save file dialog doesn't appear.
Which is the issue? How can I fix this?
I use only built-in node modules, so I don't use express.