Javascript: Download data to file from content within the page -


setting following: have homepage display diagram has been constructed using comma seperated values within page. i'd give users possibility download data cvs-file without contacting server again. (as data there) somehow possible? i'd prefer pure javascript solution.

so far i've discovered: http://pixelgraphics.us/downloadify/test.html involves flash i'd avoid.

i can't imagine question hasn't been asked before. i'm sorry double post, seems i've used wrong keywords or - haven't found solution in these forums.

update:

time changes things ;-) when first answered question ie8 latest ie browser available (nov 2010) , there no cross browser way accomplish without round trip server, or using tool requiring flash.

@zectburno's answer need now, historical context aware of ie browsers support feature.

  • btoa() undefined in ie8 , ie9
  • blob available in ie10+

be sure test in browsers need support. though blob example in other answer should work in ie10+ doesn't work me clicking link (browser nothing, no error)... if right click , save target "file.csv" navigate file , double-click can open file.

test both approaches (btoa/blob) in jsfiddle. (here's code)

<!doctype html> <html> <head> </head> <body>   <a id="a" target="_blank">download csv (via btoa)</a>   <script>     var csv = "a,b,c\n1,2,3\n";     var = document.getelementbyid("a");     a.href = "data:text/csv;base64," + btoa(csv);   </script>   <hr/>   <a id="a2" download="download.csv" type="text/csv">download csv (via blob)</a>   <script>     var csv = "a,b,c\n1,2,3\n";     var data = new blob([csv]);     var a2 = document.getelementbyid("a2");     a2.href = url.createobjecturl(data);   </script> </body> </html> 

original answer:

i don't think there option available this.

i adjust code such if flash 10+ detected (93% saturation of september 2009) on user's system, provide downloadify option, otherwise fallback server-side request.


Comments

Popular posts from this blog

android - Spacing between the stars of a rating bar? -

aspxgridview - Devexpress grid - header filter does not work if column is initially hidden -

c# - How to execute a particular part of code asynchronously in a class -