In my current script, I am downloading binary data using XHR in the content script and sending it to the background script:
let me = this;
let xhr = new XMLHttpRequest();
xhr.open('GET', url);
xhr.responseType = 'arraybuffer';
xhr.onload = function(event) {
if (this.status == 200) {
me.binaryData = {
data: xhr.response,
contentType: xhr.getResponseHeader('Content-Type')
};
}
};
xhr.send();
... later ...
sendResponse({data: me.binaryData});
Once the data is received in the background script, I want to make another XHR request to upload this binary data to the server. This is how I attempt to do it:
let formData = new FormData();
let blobBuilder = new WebKitBlobBuilder();
blobBuilder.append(data.data);
formData.append("data", blobBuilder.getBlob(data.contentType));
let request = new XMLHttpRequest();
request.open("POST", serverUrl);
request.send(formData);
The issue I encountered is that the file uploaded to the server only contains the string: "[object Object]". I suspect that ArrayBuffer type is somehow lost during the transfer from the content process to the background. How can I address this problem?