Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Create .csv and send as response in express/koa

I have some data in csv format and I want to send it as a file attachment (example.csv) to an http response without saving it as a temp file on the fs. Can this be done in express/koa?

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

If you have constructed a string of comma-separated values organized into lines separated by "\n", you can send it as an attachment with express middleware like the following:

app.get("/csvdownload", function(req, res) {
  var csv = "A,B\n1,2\n";
  res.set("Content-Disposition", "attachment; filename=whatever.csv");
  res.type("csv");
  res.end(csv);
});
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading