I have a difficult question to you, which i'm struggeling on for some time now.
I'm looking for a solution, where i can save a file to the users computer, without the local storage, because local storage has 5MB limit. I want the "Save to file"-dialog, but the data i want to save is only available in javascript and i would like to prevent sending the data back to the server and then send it again.
The use-case is, that the service im working on is saving compressed and encrypted chunks of the users data, so the server has no knowledge whats in those chunks and by sending the data back to the server, this would cause 4 times traffic and the server is receiving the unencrypted data, which would render the whole encryption useless.
I found a javascript function to save the data to the users computer with the "Save to file"-dialog, but the work on this has been discontinued and isnt fully supported. It's this: http://www.w3.org/TR/file-writer-api/
So since i have no window.saveAs, what is the way to save data from a Blob-object without sending everything to the server?
Would be great if i could get a hint, what to search for.
I know that this works, because MEGA is doing it, but i want my own solution :)
Your best option is to use a blob url (which is a special url that points to an object in the browser's memory) :
Now you have the choice to simply redirect to this url (
window.location.replace(blobUrl)
), or to create a link to it. The second solution allows you to specify a default file name :FileSaver.js implements
saveAs
for certain browsers that don't have ithttps://github.com/eligrey/FileSaver.js
Tested with FileSaver.js 1.3.8 tested on Chromium 75 and Firefox 68, neither of which have
saveAs
.The working principle seems to be to just create an
<a
element and click it with JavaScript oh the horrors of the web.Here is a demo that save a blob generated with
canvas.toBlob
to your download folder with the chosen namemypng.png
:Here is an animated version that downloads multiple images: Convert HTML5 Canvas Sequence to a Video File
See also:
From a file picker or input type=file file chooser, save the filename to local storage:
HTML:
JavaScript:
Then later in your code, when you want to play the file you selected, use the following, which gets the file using only it's name from the music library. (In the UWP package manifest, set your 'Capabilites' to include 'Music Library'.)