Skip to content Skip to sidebar Skip to footer

Html5 File Api - Slicing Or Not?

There are some nice examples about file uploading at HTML5 Rocks but there are something that isn't clear enough for me. As far as i see, the example code about file slicing is get

Solution 1:

Both Chrome and FF support File.slice() but it has been prefixed as File.webkitSlice()File.mozSlice() when its semantics changed some time ago. There's another example of using it here to read part of a .zip file. The new semantics are:

Blob.webkitSlice( 
  inlonglong start, 
  inlonglongend, 
  in DOMString contentType 
); 

Are you safe without slicing it? Sure, but remember you're reading the file into memory. The HTML5Rocks tutorial offers chunking the upload as a potential performance improvement. With some decent server logic, you could also do things like recovering from a failed upload more easily. The user wouldn't have to re-try an entire 500MB file if it failed at 99% :)

Solution 2:

This is the way to slice the file to pass as blobs:

function readBlob() {
    varfiles= document.getElementById('files').files;
    varfile= files[0];
    varONEMEGABYTE=1048576;
    varstart=0;
    varstop= ONEMEGABYTE;

    varremainder= file.size % ONEMEGABYTE;
    varblkcount= Math.floor(file.size / ONEMEGABYTE);
    if (remainder != 0) blkcount = blkcount + 1;

    for (vari=0; i < blkcount; i++) {

        varreader=newFileReader();
        if (i == (blkcount - 1) && remainder != 0) {
            stop = start + remainder;
        }
        if (i == blkcount) {
            stop = start;
        }

        //Slicing the file varblob= file.webkitSlice(start, stop);
        reader.readAsBinaryString(blob);
        start = stop;
        stop = stop + ONEMEGABYTE;

    } //End of loop

} //End of readblob

Post a Comment for "Html5 File Api - Slicing Or Not?"