使用XHR上传二进制字符串WebKit中/铬(相当于Firefox的sendAsBinary) [英] Uploading a binary string in WebKit/Chrome using XHR (equivalent to Firefox's sendAsBinary)

查看:278
本文介绍了使用XHR上传二进制字符串WebKit中/铬(相当于Firefox的sendAsBinary)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用多个尖端的WebKit功能的Web应用程序。它本质上做到这一点:读取与的FileReader ,每个文件解压缩到使用JavaScript库解压缩一个字符串,和岗位使用XMLHtt prequest每个文件的本地文件。这个伟大的工程为文本文件,但不幸的是它会损坏二进制文件(在这种情况下,图像)。火狐有解决这个问题的一个 sendAsBinary 方法,但它是非标准的,而且更重要的是,它不会对我们赖以的WebKit / Chrome浏览器其他功能。

I'm working on a webapp that uses several cutting-edge WebKit features. It essentially does this: reads a local file with the FileReader, unzips each file into a string using a JavaScript unzip library, and POSTs each file using XMLHttpRequest. This works great for text files, but unfortunately it corrupts binary files (in this case, images). Firefox has a sendAsBinary method that solves this problem, but it is non-standard, and more to the point, it doesn't work on WebKit/Chrome which we depend on for other features.

有解决方法一吨,而且至今他们没有为我工作:

There are a TON of workarounds, and so far none of them work for me:


  • 惩戒页眉,边界等等在一个长的字符串(这样的)。

  • XHR对象上设置一系列的头(这样

  • 使用 BlobBuilder ,将字符串的生成器,并使用 getBlob 来得到一个blob上传(在这个在Chrome问题线程建议

  • Mocking a file upload request with headers, boundaries, and so forth in a long string (like this).
  • Setting a bunch of headers on the xhr object (as such)
  • Using the BlobBuilder, appending the string to the builder, and using getBlob to get a blob to upload (as recommended in the Chrome issue thread about this)

我在找什么,最重要的,是一个向前兼容的解决方案。谢谢!

What I'm looking for, most of all, is a forward-compatible solution. Thanks!

推荐答案

我有同样的问题。

对于我来说这一次的工作:

This one worked for me:

XMLHttpRequest.prototype.sendAsBinary = function(datastr) {
    function byteValue(x) {
        return x.charCodeAt(0) & 0xff;
    }
    var ords = Array.prototype.map.call(datastr, byteValue);
    var ui8a = new Uint8Array(ords);
    this.send(ui8a.buffer);
}

点击这里:<一href=\"http://javascript0.org/wiki/Portable_sendAsBinary\">http://javascript0.org/wiki/Portable_sendAsBinary

这篇关于使用XHR上传二进制字符串WebKit中/铬(相当于Firefox的sendAsBinary)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆