I have a JS Array that I convert into a CSV format in the Server and send to the client by setting Content-Type: text/csv
When I send it to the client, the browser automatically downloads the file, with the file name as the last part of the URL.
For example:
If the link is /link/to/generated_csv
, then generated_csv
is automatically taken as the file name.
How do I specify that the file name as downloaded_csv.csv
?
I am using Node.js and Express.js.
router.get("/link/to/generated_csv") = function(req, res) {
let generated_csv = array2csv(source_js_array);
res.set("Content-Type", "text/csv");
// Something here to set a file name??
return res.send(generated_csv);
}
I have a JS Array that I convert into a CSV format in the Server and send to the client by setting Content-Type: text/csv
When I send it to the client, the browser automatically downloads the file, with the file name as the last part of the URL.
For example:
If the link is /link/to/generated_csv
, then generated_csv
is automatically taken as the file name.
How do I specify that the file name as downloaded_csv.csv
?
I am using Node.js and Express.js.
router.get("/link/to/generated_csv") = function(req, res) {
let generated_csv = array2csv(source_js_array);
res.set("Content-Type", "text/csv");
// Something here to set a file name??
return res.send(generated_csv);
}
Share
Improve this question
asked Oct 9, 2018 at 10:14
Sankkara NarayananSankkara Narayanan
732 silver badges6 bronze badges
2 Answers
Reset to default 7This is usually done with the Content-Disposition header.
Content-Disposition: attachment; filename="downloaded_csv.csv"
Include content-Disposition in res.set()
res.set({
"Content-Disposition": 'attachment; filename="downloaded_csv.csv"',
"Content-Type": "text/csv",
});