最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Uploading a binary string in WebKitChrome using XHR (equivalent to Firefox's sendAsBinary) - Stack Overflow

programmeradmin5浏览0评论

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:

  • 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 remended in the Chrome issue thread about this)

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

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:

  • 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 remended in the Chrome issue thread about this)

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

Share Improve this question asked Sep 18, 2010 at 19:01 heydenberkheydenberk 2972 silver badges10 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 6

I had the same problem.

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

check here: http://javascript0/wiki/Portable_sendAsBinary

You can encode it with base64 and decode it on the server.

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论