Sunday, May 19, 2024
 Popular · Latest · Hot · Upcoming
154
rated 0 times [  156] [ 2]  / answers: 1 / hits: 89569  / 11 Years ago, mon, september 9, 2013, 12:00:00

I have a situation where I need to give my users the option to save some data stored locally in their client memory to disk. The current workaround I have is having a handler like this



(define-handler (download-deck) ((deck :json))
(setf (header-out :content-type) application/json
(header-out :content-disposition) attachment)
deck)


which does exactly what it looks like. The client sends their data up, and saves the returned file locally.



This seems stupid.



Please, please tell me there's a better, simpler, cross-browser way to let a client save some local data to their disk with a file-save dialog box.



Every answer I read on the subject either says no, you can't save files with javascript or yes, there's this one semi-documented piece of the Chrome API that might let you do it in three pages.


More From » file

 Answers
39

This FileSaver library may help. If you want it to be reasonably cross-browser, you'll also need this to implement the W3C Blob API in places it's not already implemented. Both respect namespaces, and are completely framework agnostic, so don't worry about naming issues.



Once you've got those included, and as long as you're only saving text files, you should be able to



var blob = new Blob([Hello, world!], {type: text/plain;charset=utf-8});
saveAs(blob, hello world.txt);


Note that the first argument to new Blob has to be a list of strings, and that you're expected to specify the filename. As in, the user will see this file being downloaded locally, but won't be able to name it themselves. Hopefully they're using a browser that handles local filename collisions...


[#75824] Friday, September 6, 2013, 11 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
jayla

Total Points: 14
Total Questions: 96
Total Answers: 123

Location: Greenland
Member since Fri, Jul 31, 2020
4 Years ago
;