JavaEar 专注于收集分享传播有价值的技术资料

Failed to execute 'readAsArrayBuffer' on 'FileReader': parameter 1 is not of type 'Blob'

I've got a problem with a javascript Filereader which returns the error Uncaught TypeError: Failed to execute 'readAsDataURL' on 'FileReader': parameter 1 is not of type 'Blob'.

Here's the javascript:

                    var blob = null;
                    var xhr = new XMLHttpRequest(); 
                    xhr.open("GET", "C:\\Users\\yw1kew\\Desktop\\LG_FRAME.plmx"); 
                    xhr.responseType = "blob";//force the HTTP response, response-type header to be blob
                    xhr.onload = function() 
                    {
                        blob = xhr.response;//xhr.response is now a blob object
                    }
                    xhr.send();
                    var myReader = new FileReader();
                    myReader.readAsArrayBuffer(blob) // THE CODE FAILS HERE

Any idea ? Thanks

1个回答

    最佳答案
  1. In my case I use SharePoint as a platform and I needed to convert the binary code like this:

        binaryDecode = function (data) {
        var ret = '';
        if (data) {
            var byteArray = new Uint8Array(data);
            for (var i = 0; i < data.byteLength; i++) {
                ret = ret + String.fromCharCode(byteArray[i]);
            }
        }
        return ret;
    };
    

    Then use it like this:

    console.log(binaryDecode(arrayBuffer));