문제

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