File slicing in JavaScript results in empty blob

1.4k views Asked by At

I am implementing a browser-based chunked file uploader. To open the file I am using <input type="file" id="fileSelector" /> and this piece of code (simplified):

$('#fileSelector').on('change', function () {
    _file = evt.target.files[0];
});

I am slicing the file into chunks, but not reading the chunk into memory (not explicitly).

Problem: occasionally (for less than 0.1% of uploaded files) the chunk sliced from the underlying file is empty. E.g. during uploading a large file things go well, and then in the middle of that file calling:

var _blob = _file.slice(chunk.start, chunk.end, _file.type);

results in an empty slice (_blob.size is 0). Sending such blob to the server (.NET 4.6) results in Request.InputStream being empty. I am sending bniary data:

_xhr.setRequestHeader('content-type', 'application/octet-stream');
_xhr.send(_blob);

I should also mention that calling _file.slice again produces same empty blob. I can observe this in Chrome 57, Chrome 60 (Win and Mac), Mac Safari 10.1.1 and in Edge 15. Other browsers are also possible.

What can be wrong? Things I am considering:

2

There are 2 answers

1
andy250 On BEST ANSWER

The answer turned out to be very simple: that's what happens when the file being uploaded is gone (renamed, deleted).

1
Raj P V On
var offset = currentChunk * chunkSize;
        var currentFilePart = rr.files.item(0).slice(offset, (offset+chunkSize));
        var cFilePart = new FileReader();
        //var ar1 = new Uint8Array(currentFilePart.arrayBuffer());
         
        var Rajar2 = new Uint8Array(chunkSize);
        var DSA = new Uint8Array(1000);
        var DATop = 0;
        var arrTop = 0;

        var data = currentFilePart.result;
        var ar1 = new Uint8Array(data);
        alert(ar1[0]);