Dynamically generating a file with javascript?

csv, javascript

Solution

You can create a data: URI with media type `text/csv`, and either create a link to it, or navigate directly to it, in modern browsers.

It won't work in IE. (IE8 can support `data:` in limited circumstances which don't help you here.) For that browser at least, you'll need to fall back to cut-n-paste or a server backend.

Problem

I'm working on a web app for my company that will let us do some image tagging stuff, and I'd like to be able to generates results in the form of a CSV file. I can do this easily enough by dumping the CSV data to a div or something on the page and having the user copy it out. I'd rather have them hit the a generate button and have a CSV file downloaded as though they clicked on a link to the result, so they can more easily just save the file somewhere convenient. Is it possible to simulate this kind of thing with javascript? I basically want to dynamically generate the file and then let them download it, client side.

Original source