Friday, May 17, 2024
 Popular · Latest · Hot · Upcoming
138
rated 0 times [  142] [ 4]  / answers: 1 / hits: 17864  / 10 Years ago, wed, february 19, 2014, 12:00:00

Trying to initiate a browser download in Javascript, but the data I want to be downloaded is in a string, not a file. I know if it were a file, the following would do it:



window.location.href = '/filepath/file.csv';


How can I get this same effect, only with a string (with csv data), not a file that already exists on the server?


More From » javascript

 Answers
9

using my handy downloader:



<script src=http://danml.com/js/download.js></script>
<script>download(hello world, hello.txt, text/plain)</script>


you can do it without a library as well, though my lib isn't very big and supports older FF+CH and IE10:



<a id=dl download=file.txt>Download</a>
<script>
content=prompt(enter contents);
dl.href=data:text/plain,+encodeURIComponent(content);
dl.click();
</script>


EDIT: the linked script now supports window.URL.createObjectURL() for downloading files that were too big using dataURLs. I don't know the new limit, but 10mb works just file, whereas ~2mb is a limit for many dataURL ( window.open/A[download] - based ) solutions3


[#72431] Tuesday, February 18, 2014, 10 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
victorr

Total Points: 193
Total Questions: 86
Total Answers: 105

Location: Pitcairn Islands
Member since Thu, Jun 24, 2021
3 Years ago
victorr questions
Fri, Nov 13, 20, 00:00, 4 Years ago
Sat, Jul 25, 20, 00:00, 4 Years ago
Thu, Jun 11, 20, 00:00, 4 Years ago
;