question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

A way to generate and download CSV files client-side

See original GitHub issue

There seems to be a lot of confusion about how to properly generate a file client-side and have it downloaded in modern browsers. I know how to do it (it’s not hard, but it uses a modern web API) and it could potentially be a valuable addition to the library.

Something like:

Papa.download(Papa.parse(csv), "data.json");   // download JSON file
Papa.download(Papa.unparse(data), "data.csv"); // download CSV file

Might take no more than about 20-30 lines of code. Should I do it?

Issue Analytics

  • State:closed
  • Created 9 years ago
  • Reactions:5
  • Comments:43 (9 by maintainers)

github_iconTop GitHub Comments

158reactions
mholtcommented, Mar 5, 2015

This seems to work in all modern browsers:

var blob = new Blob([csvString]);
if (window.navigator.msSaveOrOpenBlob)  // IE hack; see http://msdn.microsoft.com/en-us/library/ie/hh779016.aspx
    window.navigator.msSaveBlob(blob, "filename.csv");
else
{
    var a = window.document.createElement("a");
    a.href = window.URL.createObjectURL(blob, {type: "text/plain"});
    a.download = "filename.csv";
    document.body.appendChild(a);
    a.click();  // IE: "Access is denied"; see: https://connect.microsoft.com/IE/feedback/details/797361/ie-10-treats-blob-url-as-cross-origin-and-denies-access
    document.body.removeChild(a);
}

I used this for a project at my last work. If there’s no objection, I’ll be happy to build it into 4.2 or something.

55reactions
keemorcommented, Mar 25, 2016

For modern browsers solution goes like this, tested: IE11, FF & Chrome

var csvData = new Blob([arg.data], {type: 'text/csv;charset=utf-8;'});
//IE11 & Edge
if (navigator.msSaveBlob) {
    navigator.msSaveBlob(csvData, exportFilename);
} else {
    //In FF link must be added to DOM to be clicked
    var link = document.createElement('a');
    link.href = window.URL.createObjectURL(csvData);
    link.setAttribute('download', exportFilename);
    document.body.appendChild(link);    
    link.click();
    document.body.removeChild(link);    
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

How to export JavaScript array info to csv (on client side)?
You can do this in native JavaScript. You'll have to parse your data into correct CSV format as so (assuming you are using...
Read more >
Generate CSV and Download it Client Side from the Browser
Sometimes you need to give people the ability to export CSVs. And sometimes you have all the data already in your ...
Read more >
JavaScript create and download CSV file - Javatpoint
JavaScript create and download CSV file. CSV files are an essential part of computer science when you work with websites and databases.
Read more >
How to create and download CSV - React client-side edition
How to create and download CSV - React client-side edition. Our customers always rely on Excel to dive deep into the data thus...
Read more >
How to Generate a .CSV file on the client side and save it as ...
Hi, i try to allow the client to download the data he manipulated on the client side. But i have no idea how...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found