我意识到allocation size overflow
当文件大于 200MB(类似的东西)时,新的 Mozilla Firefox 会返回(在 FileReader.ReadAsBinaryString() 上)。
这是我测试客户端 Web 浏览器的一些代码:
function upload(fileInputId, fileIndex)
{
var file = document.getElementById(fileInputId).files[fileIndex];
var blob;
var reader = new FileReader();
reader.readAsBinaryString(file);
reader.onloadend = function(evt)
{
xhr = new XMLHttpRequest();
xhr.open("POST", "upload.php", true);
XMLHttpRequest.prototype.mySendAsBinary = function(text){
var data = new ArrayBuffer(text.length);
var ui8a = new Uint8Array(data, 0);
for (var i = 0; i < text.length; i++){
ui8a[i] = (text.charCodeAt(i) & 0xff);
}
if(typeof window.Blob == "function")
{
blob = new Blob([data]);
}else{
var bb = new (window.MozBlobBuilder || window.WebKitBlobBuilder || window.BlobBuilder)();
bb.append(data);
blob = bb.getBlob();
}
this.send(blob);
}
var eventSource = xhr.upload || xhr;
eventSource.addEventListener("progress", function(e) {
var position = e.position || e.loaded;
var total = e.totalSize || e.total;
var percentage = Math.round((position/total)*100);
});
xhr.onreadystatechange = function()
{
if(xhr.readyState == 4)
{
if(xhr.status == 200)
{
console.log("Done");
}else{
console.log("Fail");
}
}
};
xhr.mySendAsBinary(evt.target.result);
};
}
所以我尝试将其更改为 FileReader.ReadAsArrayBuffer(),错误没有出现但数据不一样(因为它不是作为二进制字符串读取的)。
有没有人有任何解决方案来解决这个问题?除了 FileReader 实现之外,还有什么方法可以将更大的文件从 JS 上传到原始/字符串的 Web 服务器?
我在 Mozilla JS Documentation 上读到:
此功能是非标准的,不在标准轨道上。不要在面向 Web 的生产站点上使用它:它不适用于每个用户。实现之间也可能存在很大的不兼容性,并且行为可能会在未来发生变化。- Mozilla
如果不是 ReadAsBinaryString,如何实现 ReadAsArrayBuffer 或 ReadAsText