Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is a cross browser Save File As button possible?

I'm developing a client side music player and am looking for a way to save playlists complete with the mp3 data.

localStorage The 5mb limit for localStorage rules that option out. I'm wondering what other options there are.

DataURIs I've read about dataURIs e.g. Is there any way to specify a suggested filename when using data: URI? and Cross-browser Save As .txt but not suggesting a default filename and particularly lack of IE support are dealbreakers.

IndexedDB HTML5 indexedDB and persistence lifetime suggests that IndexedDB is not an option for persistent storage.

Are there options I am missing? I hope so!

Thanks, Andrew

like image 225
user1977132 Avatar asked Jan 22 '14 11:01

user1977132


2 Answers

I now have a solution which works in IE, Chrome and Firefox, inspired by this: Javascript set file in download

The IE10+ solution uses msSaveBlob and the code is:

if (navigator.msSaveBlob) {
    var blob_object = new Blob([store_string], {type: mime_type});
    return navigator.msSaveBlob(blob_object, filename);

    }

For Chrome/Firefox it uses the download attribute:

else if ('download' in a) {
    blob_object = new Blob ([store_string], {type: mime_type});
    a.href = URL.createObjectURL(blob_object);
    a.setAttribute("download", filename);
    a.innerHTML = "downloading...";
    d.body.appendChild(a);
    setTimeout(function() {
        a.click();
        d.body.removeChild(a);
    }, 66);
    return true;    
}

I haven't yet investigated Safari, Opera or any mobile browsers...

like image 90
user1977132 Avatar answered Oct 18 '22 22:10

user1977132


No - a cross-browser SaveAs solution is not possible.

Chrome, IE, & FF all support one (or multiple) ways to programmatically trigger the download (and saveAs dialog) of a file. Safari does not support this functionality.

like image 39
Jeff Kynaston Avatar answered Oct 18 '22 20:10

Jeff Kynaston