简体   繁体   English

如何在IE9中读取二进制数据?

[英]How to read binary data in IE9?

I'm working on some Javascript code that creates an alpha mask of a image using paths embedded by Photoshop. 我正在研究一些Javascript代码,它使用Photoshop嵌入的路径创建图像的alpha蒙版。 The onload handler of a IMG tag would call a clip(this). IMG标记的onload处理程序将调用剪辑(this)。 The function load the image's source file and scans through it. 该函数加载图像的源文件并扫描它。 Here's the setup: 这是设置:

function clip(img) {
    var xhr = new XMLHttpRequest();
    xhr.open('GET', img.src, true);
    xhr.responseType = 'arraybuffer';
    xhr.target = img;

    xhr.onload = function(e) {
        var bytes = new Uint8Array(this.response);
        var p = findPhotoshopSegment(bytes);
        if(p) {
            var paths = parse8BIMData(bytes, p);

            /* ... replaces IMG with SVG tag ... */
        }
    };
    xhr.send();
}

You can see the code in action at http://flaczki.net46.net/JPEG/SVG.html 您可以在http://flaczki.net46.net/JPEG/SVG.html上查看代码

Currently, it only works in Firefox, Chrome, and Safari. 目前,它仅适用于Firefox,Chrome和Safari。 It doesn't work in IE9. 它在IE9中不起作用。 The browser supports SVG but not Uint8Array. 浏览器支持SVG但不支持Uint8Array。 Is there some kind of workaround? 有某种解决方法吗?

I had same issue while playing with pdf.js library, and solution I found is to make my own Uint8Array, below is code which you need. 我在玩pdf.js库时遇到了同样的问题,我找到的解决方法是制作自己的Uint8Array,下面是你需要的代码。 All credits goes to notmasteryet @github and full code can be found here https://gist.github.com/1057924 所有积分都转到notmasteryet @github,完整的代码可以在这里找到https://gist.github.com/1057924

(function() {
  try {
    var a = new Uint8Array(1);
    return; //no need
  } catch(e) { }

  function subarray(start, end) {
    return this.slice(start, end);
  }

  function set_(array, offset) {
    if (arguments.length < 2) offset = 0;
    for (var i = 0, n = array.length; i < n; ++i, ++offset)
      this[offset] = array[i] & 0xFF;
  }

  // we need typed arrays
  function TypedArray(arg1) {
    var result;
    if (typeof arg1 === "number") {
       result = new Array(arg1);
       for (var i = 0; i < arg1; ++i)
         result[i] = 0;
    } else
       result = arg1.slice(0);
    result.subarray = subarray;
    result.buffer = result;
    result.byteLength = result.length;
    result.set = set_;
    if (typeof arg1 === "object" && arg1.buffer)
      result.buffer = arg1.buffer;

    return result;
  }

  window.Uint8Array = TypedArray;
  window.Uint32Array = TypedArray;
  window.Int32Array = TypedArray;
})();

If you want to be assured you get wider range of compatibility, you can also use the compatibility.js file included in the PDF.js distribution under the "web" directory. 如果您想确保获得更广泛的兼容性,还可以使用“web”目录下PDF.js发行版中包含的compatibility.js文件。 Get PDF.js from Mozilla Github , decompress the zip archive and include the compatibility.js file from the above directory. Mozilla Github获取PDF.js,解压缩zip存档并包含上述目录中的compatibility.js文件。 It also makes PDF.js library work in eg IE9 它还使PDF.js库在IE9中工作

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM