I'm trying to understand HTML5 API. I'm designing the web application where the browser client need to download multiple files from server; user will perform something with the downloaded files and the application than need to save the state on user hard-rive. I understand that the browser can save these files only to its sandbox which is fine as long as the user can retrieve those files on the second time he starts the application. Should I use BlobBuilder or FileSaver? I'm a bit lost here.
相关问题
- Views base64 encoded blob in HTML with PHP
- Zend/PHP: Problem uploading/downloading file to/fr
- How to store .net core wwwroot files in azure blob
- ie9 Error: 'Blob' is undefined
- Using Dapper with BLOBs and SQL Server CE
相关文章
- HTML5 control
- window.resolveLocalFileSystemURL vs window.request
- Canvas to blob on Edge
- React Native Uploading File in parts using XMLHttp
- Resumable.js: Persist file uploading between redir
- HTML5 and [removed] Opening and Reading a Local Fi
- Access blob file using time stamp in Azure
- [removed] convert BASE64 to BLOB fails in safari o
My trick is to simply append IFRAMEs with a "src" attribute pointing to your multiple downloads. The server site should send the files with a "disposition: attachment" header and then the client will try to store the file locally. The only "problem" is that the IFRAMEs will stay in your DOM tree as debris until the user leaves or reloads the page. Make the IFRAME invisible (e.g. width=0; height=0;) and you are ready to go! All browsers.
If you only support HTML5 browsers, there's a "download" attribute you can use. More details here : http://updates.html5rocks.com/2011/08/Downloading-resources-in-HTML5-a-download
I'm going to show you how to download files with the XMLHttpRequest Level 2 and save them with the FileSystem API or with the FileSaver interface.
Downloading Files
To download a file you will use the XMLHttpRequest Level 2 (aka XHR2), which supports cross-origin requests, uploading progress events, and uploading/downloading of binary data. In the post "New Tricks in XMLHttpRequest2" there's plenty of examples of use of XHR2.
To download a file as a blob all you have do to is specify the
responseType
to "blob". You can also use the types "text", "arraybuffer" or "document". The function below downloads the file in theurl
and sends it to thesuccess
callback:The
success
callback will receive as argument an instance of Blob that can be later modified and saved and/or uploaded to a server.Saving Files with the FileSystem API
As the Can i use... site points out there aren't many browsers with support to the FileSystem API. For Firefox there's an explanation for the lack of support. So, you will have to use Chrome to do this.
First you will have to request a storage space, it can be either temporary or persistent. You will probably want to have a persistent storage, in this case you will have request a quota of storage space upfront (some facts):
Now that you have access to the file system you can save and read files from it. The function below can save a blob in the specified path into the file system:
An to read a file by it's path:
In addition to the
readAsText
method, according to the FileReader API you can callreadAsArrayBuffer
andreadAsDataURL
.Using the FileSaver
The post "Saving Generated Files on Client-Side" explains very well the use of this API. Some browsers may need the FileSaver.js in order to have the
saveAs
interface.If you use it together with the
downloadFile
function, you could have something like this:Of course it would make more sense if the user could visualize the image, manipulate it and then save it in his drive.
Error Handler
Just to fulfill the example:
Useful links