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.
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.
Any ideas for at least a new path of investigation would be highly appreciated.
Turns out that you can do this.
Chrome has a way to send a blob via XMLHTTPRequest.
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));
}
精彩评论