4

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:

andy250
  • 19,284
  • 2
  • 11
  • 26
  • 2
    https://stackoverflow.com/a/24834417/1606432 This gives some further insight into the File & Blob check – Pogrindis Sep 05 '17 at 13:07
  • Could you share how you calculate chunk start and end. Have you tried with Firefox/Edge? –  Sep 05 '17 at 13:59
  • @K3N Yes, tried in lots of browsers and OSes and this works 99,9% of the time. Start and end are calculated more less like this: var start = N * _chunkSize; var end = Math.min(start + _chunkSize, _file.size); Where N is the current chunk number (0-based). – andy250 Sep 06 '17 at 07:03
  • Why and how do you use a FileReader ? – Kaiido Sep 06 '17 at 09:35
  • @Kaiido - good point. We actually don't. I have updated the question now. – andy250 Sep 06 '17 at 11:19
  • 1
    @K3N I am using a customized version of plupload3. Here is chunk count calculation: https://github.com/andy250/plupload/blob/3.x/src/FileUploader.js#L84. The complete process is spread over more files in addition to FileUploader.js: https://github.com/andy250/plupload/blob/3.x/src/core/Queue.js and probably couple more. I seriously doubt these calculations are the problem though since the process usually works. I could agree that there is some variable scoping issue. – andy250 Sep 06 '17 at 13:48

1 Answers1

1

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

andy250
  • 19,284
  • 2
  • 11
  • 26
  • While this issue helped me somehow, the real solver for me was https://stackoverflow.com/questions/32898082/splitting-a-file-into-chunks-with-javascript, anyway +1 – Martin Shishkov Apr 16 '18 at 09:37