Dominic
Dominic

Reputation: 171

Unable to read SQL blob via FileReader in IE 11

I am reading a blob from my SQL database via AJAX and the code below which works fine in FireFox, Edge, and Chrome but I receive an error "invalid state error" in the debugger for the line xmlhttp.responseType = "blob"; in IE11. I have tried all sorts of combinations for xmlhttp.responseType but cannot get it to work in IE11. For example, if I just comment out xmlhttp.responseType = "blob"; I obtain "type mismatch error" for the line xmlhttp.responseType = "blob";. And was wondering if someone could help me with this. Here is the code in my html file to make the ajax request:

if (window.XMLHttpRequest) {
           // code for IE7+, Firefox, Chrome, Opera, Safari
           xmlhttp = new XMLHttpRequest();
        } else {
           // code for IE6, IE5
           xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
        }



        xmlhttp.responseType = "blob";



        xmlhttp.onreadystatechange = function()
        {
            if (this.readyState == 4 && this.status == 200)
            {
               theResponse=this.response;
               theBlobSize=theResponse.size;
               reader = new FileReader();
               reader.addEventListener("loadend", function()
               {

                   // get data from blob here 

                });
                reader.readAsArrayBuffer(theResponse);

               }
            };
            xmlhttp.open("POST","getImagAlgebraicBlob.php",true);
            xmlhttp.send();
        }

And here is the php file "getImagAlgebraicBlob.php" being called to read the blob using PDO and is very simple and again, works perfectly in the other browsers:

<?php

include 'algebraicFunctionBlobClass.php';

$blobObj = new algebraicFunctionBlob();

$a = $blobObj->selectImagBlob(132);

echo $a['imagWebGLData'];
?>

Thanks,

Upvotes: 1

Views: 743

Answers (1)

Endless
Endless

Reputation: 37915

IE6/5 are long gone so you don't need to do special treatment for that

you don't have to use the FileReader if you set the responseType to arraybuffer directly...

var xhr = new XMLHttpRequest

xhr.onload = function() {
  var buffer = this.response
  var size = buffer.byteLength

  // construct the arraybuffer as a blob if you ever need it
  // var blob = new Blob([buffer])
}

xhr.open('POST', 'getImagAlgebraicBlob.php')
xhr.responseType = 'arraybuffer'
xhr.send()

Upvotes: 1

Related Questions