Monday, 15 March 2010

Save file Javascript with file name -



Save file Javascript with file name -

i'm working on text editor in pure javascript. i'd when user clicks 'save' button, editor downloads file. i've got partly working:

uricontent = "data:application/octet-stream," + encodeuricomponent(codemirror.getvalue()); newwindow=window.open(uricontent, 'filename.txt');

the file downloads, problem file named 'download'.

question: how alter name of file want, e.g filename.txt?

replace "save" button anchor link , set new download attribute dynamically. works in chrome , firefox:

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

here's working illustration name set current date: http://jsfiddle.net/qjvb3/

here compatibility table downloadattribute: http://caniuse.com/download

javascript

No comments:

Post a Comment