将Blob转换为数组

时间:2017-06-27 08:04:27

标签: javascript xmlhttprequest

我试图将收到的Blob数据转换为数组,如下所示:

var xhr = new XMLHttpRequest();
xhr.open('GET', "chrome://favicon/http://www.cnn.com/", true);
xhr.responseType = 'blob';

xhr.onload = function (e) {
    if (this.status == 200) {
        var favicon = new Uint8Array(this.response);
    }
};

xhr.send();

但是这句话:

new Uint8Array(this.response);

不会产生数组。这里有什么问题?

1 个答案:

答案 0 :(得分:0)

想出来:

    var xhr = new XMLHttpRequest();
    xhr.open('GET', "chrome://favicon/http://www.cnn.com/", true);
    xhr.responseType = 'blob';

    xhr.onload = function (e) {
        if (this.status == 200) {
            // get binary data as a response
            //var blob = this.response;
            //var favicon = new Uint8Array(this.response);

            var reader = new FileReader();
            reader.onload = function (e) {
                mDataToTransmit.favicon = e.target.result;
                transmitData();
            };

            reader.readAsDataURL(this.response);
        }
    };

    xhr.send();