JavaScript中的文件切片导致空blob [英] File slicing in JavaScript results in empty blob
问题描述
我正在实现一个基于浏览器的分块文件上传器。要打开文件,我使用< input type =fileid =fileSelector/>
和这段代码(简化):
$('#fileSelector')。on('change',function(){
_file = evt.target.files [0 ];
});
我正在将文件切成块,但不将块读入内存(未明确)。 / p>
问题:偶尔(少于0.1%的上传文件)从基础文件中切出的块是空的。例如。在上传大文件的过程中,事情进展顺利,然后在该文件的中间调用:
var _blob = _file.slice( chunk.start,chunk.end,_ file.type);
产生一个空切片( _blob.size
是0)。将此blob发送到服务器(.NET 4.6)会导致 Request.InputStream
为空。我发送的是bniary数据:
_xhr.setRequestHeader('content-type','application / octet-stream');
_xhr.send(_blob);
我还应该提到调用 _file.slice
再次产生相同的空斑点。我可以在Chrome 57,Chrome 60(Win和Mac),Mac Safari 10.1.1和Edge 15中观察到这一点。其他浏览器也是可以的。
什么可以错误?我正在考虑的事情:
- 文件正在使用中
- 文件/磁盘已损坏
- 同时使用JavaScript引擎对blob进行GC管理 - 如果可以的话,如何确保它不是GC?
- 浏览器错误(我找到了)这个,但不确定它是否相关: https://错误。 chromium.org/p/chromium/issues/detail?id=167111 )
答案结果非常简单:当上传的文件消失(重命名,删除)时会发生这种情况。
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:
- file is in use
- file/disk is corrupt
- the blob is GCed by JavaScript engine in the meantime - if that can be the case how to make sure it is not GCed?
- browser bug (I found this, but not sure if it is related: https://bugs.chromium.org/p/chromium/issues/detail?id=167111)
The answer turned out to be very simple: that's what happens when the file being uploaded is gone (renamed, deleted).
这篇关于JavaScript中的文件切片导致空blob的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!