【问题标题】:Unable to read SQL blob via FileReader in IE 11无法通过 IE 11 中的 FileReader 读取 SQL blob
【发布时间】:2017-03-16 01:32:39
【问题描述】:

我正在通过 AJAX 从我的 SQL 数据库中读取一个 blob,下面的代码在 FireFox、Edge 和 Chrome 中运行良好,但我在调试器中为 xmlhttp.responseType 行收到错误“无效状态错误” = "blob"; 在 IE11 中。我已经尝试了 xmlhttp.responseType 的各种组合,但无法让它在 IE11 中工作。例如,如果我只是注释掉 xmlhttp.responseType = "blob"; 我会为行 xmlhttp.responseType = "blob"; 获得“类型不匹配错误”。并且想知道是否有人可以帮助我。这是我的 html 文件中用于发出 ajax 请求的代码:

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();
        }

这里是调用 php 文件“getImagAlgebraicBlob.php”以使用 PDO 读取 blob,并且非常简单,并且在其他浏览器中也能完美运行:

<?php

include 'algebraicFunctionBlobClass.php';

$blobObj = new algebraicFunctionBlob();

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

echo $a['imagWebGLData'];
?>

谢谢,

【问题讨论】:

    标签: javascript php sql ajax


    【解决方案1】:

    IE6/5 早已不复存在,因此您无需为此做特殊处理

    如果直接将 responseType 设置为 arraybuffer,则不必使用 FileReader...

    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()
    

    【讨论】:

    • 我尝试使用 'arraybuffer' 并删除了阅读器,它确实适用于 chrome、firefox 和 edge 但我仍然收到错误消息 'xmlhttp.responseType=' 行的错误消息 'invalid state error' arraybuffer' 当我在 IE11 中运行它时。
    • 尝试在open() - stackoverflow.com/questions/20760635/…之后设置responseType
    猜你喜欢
    • 2011-07-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-23
    • 1970-01-01
    相关资源
    最近更新 更多