snippetjavascriptCritical
How to create a file in memory for user to download, but not through server?
Viewed 0 times
downloadhowforuserserverfilenotbutthroughmemory
Problem
Is there a way to create a text file on the client side and prompt the user to download it without any interaction with the server?
I know I can't write directly to their machine (security and all), but can I create the file and prompt them to save it?
I know I can't write directly to their machine (security and all), but can I create the file and prompt them to save it?
Solution
You can use data URIs. Browser support varies; see Wikipedia. Example:
The octet-stream is to force a download prompt. Otherwise, it will probably open in the browser.
For CSV, you can use:
Try the jsFiddle demo.
text fileThe octet-stream is to force a download prompt. Otherwise, it will probably open in the browser.
For CSV, you can use:
CSV OctetTry the jsFiddle demo.
Code Snippets
<a href="data:application/octet-stream;charset=utf-16le;base64,//5mAG8AbwAgAGIAYQByAAoA">text file</a><a href="data:application/octet-stream,field1%2Cfield2%0Afoo%2Cbar%0Agoo%2Cgai%0A">CSV Octet</a>Context
Stack Overflow Q#3665115, score: 490
Revisions (0)
No revisions yet.