[英]Javascript: Sending arrayBuffer using XMLHttpRequest
I want to send a multipart form using XMLHttpRequest. 我想使用XMLHttpRequest发送一个multipart表单。 The file I want to attach is a jpg file.
我要附加的文件是一个jpg文件。 Appending the file to the FormData object works fine.
将文件附加到FormData对象可以正常工作。
But I'd like to process the image file before sending it. 但我想在发送之前处理图像文件。 Therefore I have a library that takes a Uint8Array as input and output as well.
因此,我有一个库,它将Uint8Array作为输入和输出。 So I have the processed image as UInt8Array.
所以我将处理后的图像作为UInt8Array。
I tried to use 我试着用
form.append("picture", new Blob(fileAsArray, {type: "image/jpg"} ));
but it creates an octet/stream. 但它会创建一个八位字节/流。 So how can I send the Uint8Array via XMLHttpRequest multipart/form so that the server sees the same as when sending the file object?
那么如何通过XMLHttpRequest multipart / form发送Uint8Array,以便服务器看到与发送文件对象时相同的内容?
Notice that the Blob
constructor takes an array of typed arrays (or other sources) as its parameter. 请注意,该
Blob
的构造需要键入阵列(或其它来源)作为它的参数的阵列 。 Try 尝试
form.append("picture", new Blob([fileAsArray], {type: "image/jpg"} ));
Probably it isn't the direct response to the question, but I have created a function to upload an ArrayBuffer as a file using not XMLHttpRequest, but fetch. 可能它不是对问题的直接回应,但是我已经创建了一个函数来将ArrayBuffer作为文件上传而不是使用XMLHttpRequest,而是使用fetch。
So here is my version with Javascript Fetch: 所以这是我的Javascript Fetch版本:
function uploadArrayBufferRawImage(arraybuffer)
{
var formData = new FormData();
formData.append("image",
new Blob([arraybuffer], {type: "image/x-myrawformat-raw"}),
new Date().toJSON() + ".raw");
fetch("scripts/store_image.php", {
method: 'POST',
body: formData
}).then(function(resp)
{
return resp.json();
}).then(function(json)
{
console.log(json);
});
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.