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

javascript - Specify a file name in http headers while sending file to client via http - Stack Overflow

programmeradmin3浏览0评论

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

2 Answers 2

Reset to default 7

This 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",
});
发布评论

评论列表(0)

  1. 暂无评论