发表在JavaScript中的二进制数据跨域 [英] Post binary data cross domain in javascript
问题描述
我正在写一个Chrome浏览器扩展,采用当前选项卡的视图,并上传它的快照到Web服务的API,我没有控制。在Chrome扩展库有一个函数(chrome.tabs.captureVisibleTab见的http:/ /$c$c.google.com/chrome/extensions/tabs.html ),其拍照并返回数据在数据的URL。我陷入了僵局,如何获得这些数据上传。
I'm writing a Chrome browser extension that takes a snapshot of the current tab's view and uploads it to a web service API that I don't control. The Chrome extension library has a function (chrome.tabs.captureVisibleTab. see http://code.google.com/chrome/extensions/tabs.html) that takes a snapshot and returns the data in a data url. I'm at an impasse as to how to get that data uploaded.
我试着写我自己的多部分形式的请求,并使用一个Ajax请求POST数据。但是,阿贾克斯坚持UTF-8编码的数据和API坚持8位的EN codeD二进制文件。我想用一个文件上传插件,如 http://malsup.com/jquery/form/ 也许会工作,但我似乎无法得到的JS变量中的数据到一个表单上载者会抓住。
I've tried to write my own multipart-form request and use an ajax request to POST the data. But, ajax insists on UTF-8 encoding the data and the API insists on 8-bit encoded binary. I thought maybe using a file uploader plugin like http://malsup.com/jquery/form/ would work, but I can't seem to get the data from the JS variable into a form the uploader will take.
任何想法进行调查,至少一条新的道路将是非常美联社preciated。
Any ideas for at least a new path of investigation would be highly appreciated.
推荐答案
原来,你可以做到这一点。
Turns out that you can do this.
Chrome浏览器有办法通过XMLHTT prequest发送一个blob。
Chrome has a way to send a blob via XMLHTTPRequest.
下面是从铬问题跟踪链接,例如:code:
Here's a link to example code from the Chromium issue tracker:
的http:// code。 google.com/p/chromium/issues/detail?id=35705#c34
XMLHttpRequest.prototype.sendAsBinary = function(datastr,contentType) {
var bb = new BlobBuilder();
var len = datastr.length;
var data = new Uint8Array(len);
for (var i=0; i<len; i++) {
data[i] = datastr.charCodeAt(i);
}
bb.append(data.buffer);
this.send(bb.getBlob(contentType));
}
这篇关于发表在JavaScript中的二进制数据跨域的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!