在javascript中发布二进制数据跨域

时间:2022-08-23 10:07:11

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.

我正在编写一个Chrome浏览器扩展程序,其中包含当前标签视图的快照,并将其上传到我无法控制的Web服务API。 Chrome扩展程序库具有一个功能(chrome.tabs.captureVisibleTab。请参阅http://code.google.com/chrome/extensions/tabs.html),该功能可获取快照并返回数据网址中的数据。关于如何上传数据,我陷入了僵局。

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.

我试图编写自己的多部分表单请求并使用ajax请求来POST数据。但是,ajax坚持使用UTF-8编码数据,并且API坚持使用8位编码二进制文件。我想也许使用像http://malsup.com/jquery/form/这样的文件上传器插件可以工作,但我似乎无法将JS变量中的数据转换为上传者将采用的形式。

Any ideas for at least a new path of investigation would be highly appreciated.

对于至少一条新的调查路径的任何想法都将受到高度赞赏。

1 个解决方案

#1


2  

Turns out that you can do this.

事实证明你可以做到这一点。

Chrome has a way to send a blob via XMLHTTPRequest.

Chrome有一种通过XMLHTTPRequest发送blob的方法。

Here's a link to example code from the Chromium issue tracker:

以下是Chromium问题跟踪器中示例代码的链接:

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

#1


2  

Turns out that you can do this.

事实证明你可以做到这一点。

Chrome has a way to send a blob via XMLHTTPRequest.

Chrome有一种通过XMLHTTPRequest发送blob的方法。

Here's a link to example code from the Chromium issue tracker:

以下是Chromium问题跟踪器中示例代码的链接:

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