Sunday, May 19, 2024
 Popular · Latest · Hot · Upcoming
186
rated 0 times [  191] [ 5]  / answers: 1 / hits: 121252  / 13 Years ago, mon, october 10, 2011, 12:00:00

I'm working on a text editor in pure Javascript. I'd like it so that when the user clicks the 'Save' button, the editor downloads the file. I've already got this partly working:



uriContent = data:application/octet-stream, + encodeURIComponent(codeMirror.getValue());
newWindow=window.open(uriContent, 'filename.txt');


The file downloads, but the problem is that the file is named 'download'.



Question: How could I change the name of the file to be anything I want, e.g filename.txt?


More From » javascript

 Answers
71

Replace your Save button with an anchor link and set the new download attribute dynamically. Works in Chrome and Firefox:



var d = ha;
$(this).attr(href, data:image/png;base64,abcdefghijklmnop).attr(download, file- + d + .png);


Here's a working example with the name set as the current date: http://jsfiddle.net/Qjvb3/



Here a compatibility table for downloadattribute: http://caniuse.com/download


[#89692] Sunday, October 9, 2011, 13 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
rocky

Total Points: 316
Total Questions: 108
Total Answers: 110

Location: Mali
Member since Sat, Feb 12, 2022
2 Years ago
;