1. 程式人生 > 其它 >FileReader error: The object is already busy reading Blobs

FileReader error: The object is already busy reading Blobs

在使用多個FileReader物件時候會遇到。

FileReader錯誤:物件已經忙於讀取Blob。 

    var blob = this.file.slice(this.readed, this.readed + this.step);
            this.reader.readAsArrayBuffer(blob);

 

解決方案:

reader讀取時候,先判斷一下readyState值,如果不是loading的話再執行讀取。

    if (this.reader.readyState == 1)
                return;
            
var blob = this.file.slice(this.readed, this.readed + this.step); this.reader.readAsArrayBuffer(blob);

 

 

 

readyState FileReader

價值 狀態 描述
0 EMPTY 閱讀器已建立。尚未呼叫任何讀取方法。
1 LOADING 已呼叫讀取方法。
2 DONE 操作完成。
EMPTY

FileReader建立,但尚未呼叫 readAs 方法。

LOADING

呼叫了 readAs 方法。正在讀取一個FileBlob

,但尚未發生錯誤。

DONE

讀取操作完成。這可能意味著:整個FileBlob已讀入記憶體,發生檔案讀取錯誤,或被abort()呼叫而讀取被取消。

var reader = new FileReader();
console.log('EMPTY', reader.readyState); // readyState will be 0

reader.readAsText(blob);
console.log('LOADING', reader.readyState); // readyState will be 1

reader.onloadend = function () {
  console.log(
'DONE', reader.readyState); // readyState will be 2 };

 

 

更多:

判斷瀏覽器是否支援FileReader

HTML5 檔案域+FileReader 分段讀取檔案並上傳(八)-WebSocket

HTML5 檔案域+FileReader 分段讀取檔案並上傳(七)-WebSocket