【问题标题】:httprequest and binary data in javascriptjavascript中的httprequest和二进制数据
【发布时间】:2012-05-31 01:30:30
【问题描述】:

在多次尝试读取作为 jpg 图像的二进制数据流的 httprequest 的响应后仍然苦苦挣扎。

编辑:整件事

xmlhttp = false;
        /*@cc_on@*/
        /*@if (@_jscript_version >= 5)
        // JScript gives us Conditional compilation, we can cope with old IE versions.
        // and security blocked creation of the objects.
        try {
            xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
        } catch (e) {
            try {
                xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
            } catch (E) {
                xmlhttp = false;
            }
        }
        @end@*/
        if (!xmlhttp && typeof XMLHttpRequest != 'undefined') {
            try {
                xmlhttp = new XMLHttpRequest();
            } catch (e) {
                xmlhttp = false;
            }
        }
        if (!xmlhttp && window.createRequest) {
            try {
                xmlhttp = window.createRequest();
            } catch (e) {
                xmlhttp = false;
            }
        }

        xmlhttp.open("GET", theUrl, true);  
        xmlhttp.onreadystatechange = function () {
            if (xmlhttp.readyState == 4) {

                var headers = xmlhttp.getAllResponseHeaders(); 
            }
        }

        xmlhttp.send(null);

我使用的是 IE8,没有 HTML 5(也在 FF12 中尝试过) 所以我总是会遇到类似的错误

xhr.overrideMimeType('text\/plain; charset=x-user-defined');

xhr.responseType = "arraybuffer";

即使复制到变量中也不起作用

var body = xhr.response; //.responseText , .responseBody

任何想法有什么问题或我可以尝试什么?

【问题讨论】:

  • 为什么?你想达到什么目的?你的服务器可以做 b64 编码吗?
  • 不,它是嵌入式设备,公司不会遗憾地改变他们的界面
  • 那么你应该如何在脚本端使用二进制数据呢?
  • 计划是从图像数据流中创建图像

标签: javascript html xmlhttprequest binary-data responsetext


【解决方案1】:

我让this example 在客户端读取二进制 JPEG 并解析 EXIF 数据。它使用BinFileReader.js,这使得处理二进制数据非常容易,跨浏览器。 Here is the whole thing in a zip file,包括一个基于节点的网络服务器(使用 node server.js 运行)我还包括一个 web-worker example

如果您只是从二进制流中制作图像,为什么不将<img src="blah" /> 添加到您的 DOM 中?

$('body').append('<img src="' + URL + '"/>');

【讨论】:

  • 这能回答你的问题吗?
猜你喜欢
  • 2015-06-20
  • 2011-10-02
  • 1970-01-01
  • 2011-11-23
  • 2012-02-23
  • 2011-08-18
  • 2011-01-07
  • 2012-06-18
  • 1970-01-01
相关资源
最近更新 更多