使用XMLHttpRequest下载二进制数据,不使用overrideMimeType

fun*_*bro 6 javascript qt xmlhttprequest qml

我正在尝试使用Javascript检索图像的数据XMLHttpRequest.

var xhr = new XMLHttpRequest();
xhr.open("GET", "http://www.celticfc.net/images/doc/celticcrest.png");
xhr.onreadystatechange = function() {
    if (xhr.readyState === 4) {
        var resp = xhr.responseText;
        console.log(resp.charCodeAt(0) & 0xff);
    }
};
xhr.send();
Run Code Online (Sandbox Code Playgroud)

这个数据的第一个字节应该是0x89,但是任何高值字节都返回为0xfffd(0xfffd & 0xff是0xfd).

诸如此类的问题这一个使用提供解决方案overrideMimeType()的功能,但是这不支持的平台上,我使用(QT/QML).

如何正确下载数据?

use*_*064 5

Google I/O 2011:面向Web开发人员的HTML5展示:哇哇和如何

获取二进制文件:新的热度

var xhr = new XMLHttpRequest();
xhr.open('GET', 'http://www.celticfc.net/images/doc/celticcrest.png', true);

xhr.responseType = 'arraybuffer';

xhr.onload = function(e) {
   if (this.status == 200) {
       var uInt8Array = new Uint8Array(this.response); // Note:not xhr.responseText

       for (var i = 0, len = uInt8Array.length; i < len; ++i) {
           uInt8Array[i] = this.response[i];
       }

       var byte3 = uInt8Array[4]; // byte at offset 4
   }
}

xhr.send();
Run Code Online (Sandbox Code Playgroud)

  • 不适合我,`xhr.response`未定义. (3认同)