On my Node.js server, I have a route that requests data from the database, and converts it from JSON to CSV.

I then state the content type is application/csv, and serve the file.

var csvBlob = json2csv(jsonBlob);
res.writeHead(200, {'Content-Type': 'text/csv' });
res.end(csvBlob);

When I get the file on my client side, it just comes out as 'filename', with no file extension. How do I serve the file with the extension?

Note:

My client doesn't necessarily send accept headers asking for CSV, as the request can be called as a simple browser URL, such as:

http://myserver.org/resources/mydbquery/xls
http://myserver.org/resources/mydbquery/csv
http://myserver.org/resources/mydbquery/pdf
有帮助吗?

解决方案

Try this.

res.writeHead(200, {
    'Content-Type': 'text/csv'
    , 'Content-Disposition': 'attachment; filename=download.csv'
});
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top