有一個應用程序,其中有一個類型爲「file」的輸入。以下方法獲取文件,然後準備通過AJAX將其發送到服務器。爲大文件轉換Uint8Array崩潰瀏覽器
private StartUpload = (files) => {
if (files && files.length === 1) {
this.GetFileProperties(files[0])
.done((properties: IFileProperties) => {
$('input[type=file]').val("");
if (this._compatibleTypes.indexOf(properties.Extension) >= 0) {
var base64 = this.ArrayBufferToBase64(properties.ArrayBuffer);
this.DoFileUpload(base64, properties.Extension).always(() => {
this.ShowDialogMessage('edit_document_upload_complete', 'edit_document_upload_complete');
});
} else {
this.ShowDialogMessage('edit_document_upload_incompatible', 'edit_document_upload_compatible_types', this._compatibleTypes);
}
});
} else {
this.ShowDialogMessage('edit_document_upload_one_file', 'edit_document_upload_one_file_msg');
}
};
private ArrayBufferToBase64(buffer): any {
var binary = '';
var bytes = new Uint8Array(buffer);
for (var xx = 0, len = bytes.byteLength; xx < len; xx++) {
binary += String.fromCharCode(bytes[xx]);
}
return window.btoa(binary);
}
private DoFileUpload = (base64, extension) => {
this.IsLoading(true);
var dfd = $.Deferred();
var data = {
data: base64
};
UpdateFormDigest((<any>window)._spPageContextInfo.webServerRelativeUrl, (<any>window)._spFormDigestRefreshInterval);
var methodUrl = "_vti_bin/viewfile/FileInformation.asmx/AddScannedItemAlt";
$.ajax({
headers: {
"X-RequestDigest": $("#__REQUESTDIGEST").val()
},
url: methodUrl,
contentType: "application/json",
data: JSON.stringify(data),
dataType: 'json',
type: "POST",
success: (response) => {
// do stuff
},
error: (e) => {
// do stuff
}
});
return dfd;
};
這在絕大多數情況下都能很好地工作。但是,當文件大小很大時(比如200MB +),會導致瀏覽器死機。
Chrome顯示帶有「aw snap」消息的黑灰色頁面,基本死亡。
IE顯示「內存不足」控制檯錯誤,但繼續工作。
FF顯示「無響應的腳本」警告。選擇「不要再顯示我」讓它運行,直到出現「內存不足」控制檯錯誤。
這就是它死:
for (var xx = 0, len = bytes.byteLength; xx < len; xx++) {
binary += String.fromCharCode(bytes[xx]);
}
結束語一個try/catch解決此不做任何動作,被捕獲任何錯誤。
我可以在沒有崩潰的情況下進入循環,但是從len = 210164805開始,每次迭代都很困難。爲此,我嘗試將console.log(xx)添加到循環中並讓它飛 - 但瀏覽器崩潰在任何事情出現在日誌之前。
字符串的大小是否有一定的限制,可能導致瀏覽器在超過時崩潰?
謝謝