Earlier in this blog I talked about the general idea behind one solution for uploading very large files (I consider very large when they are above 2GB), how the file API is becoming more popular and the possible client side approaches. This time I’ll describe the JavaScript client implementation part of the solution. As a quick reminder, the client is responsible for:
- Prompt the file dialog and store locally the metadata related to the file to be uploaded.
- Send each packet accordingly with its id, the current packet number and the total packets.
- Receive the ACK from each packet and send the last received + 1 unless this is the last one.
The initial implementation, some kind of proof of concept, was made using HTML5 with the FileReader API, but as this cannot be the only implementation, it was necessary to let open an “interface” so any other technology for further implementations can be done without the risk of touching unnecessary code fragments.
The object was named $.uploaderHtml5 as a jquery extension and the interface was defined as follows:
{ // initialization code init: function () { }, // return the visual element associated getDOMElement: function () { }, // return true if there's a selected file hasFile: function () { }, // return the file name from the input dialog getFileName: function () { }, // return the file size from the input dialog getFileSize: function () { }, // stop the current upload cancelUpload: function () { }, // start the current upload startUpload: function () { } }
In the HTML5 implementation there are of course more internal functions that deal with the proper send and receive packets actions, because it’s responsible for all the verifications such as parse the ACK from the server and take action accordingly, the POST to the server must be done with all the data required so the server can interpret it and ask for a new piece.
The particularly interesting point here is how the file API is used to deal with the partial and vendor-dependent implementation, the “standard” method slice has three known variants: mozSlice, webkitSlice and slice, so there’s no choice that ask for each of them.
if ('mozSlice' in this.ufile) { // mozilla packet = this.ufile.mozSlice(startByte, endByte); } else if ('webkitSlice' in this.ufile) { // webkit packet = this.ufile.webkitSlice(startByte, endByte); } else { // IE 10 packet = this.ufile.slice(startByte, endByte); } return packet;
Another HTML5 element used in this code is the FormData object which is really easy to use for send post data to the server; in addition, the XMLHttpRequest has the ability of uploading files which is very important in order to simplify the code, otherwise an iframe should have been set up with a form inside with an input type=file.
The whole source code can be found at https://bitbucket.org/abelperezok/freshupload.
No comments:
Post a Comment