Search code examples
javascripthtmlfileapi

File API, programaticly download file from server and store it in sandboxed file system


Is there a way how to programmaticly download the file from the server (user clicks save button in the web app) and the browser(supporting File API) or device will download the file and save it in its sand-boxed file system? I'm looking at the HTML5 File API. I can create or amend the file and store it, but first I need to get the file from server. Simply said I want to avoid the save as pop up window and I want the file stay within sandboxed file system of web application. Any suggestions where I can look is welcomed.


Solution

  • You want XMLHttpRequest(), which despite its name, can handle downloading all types of data including binary. If you set the responseType to "arraybuffer", you can convert that to a blob and save it to the file system pretty easily.

    HTML5Rocks has a great tutorial that should cover everything you need: New Tricks in XMLHttpRequest

    (I know this question is a bit old, but I was just searching for how to do the same thing and it was the first result that popped up, so I thought I'd share)