简体   繁体   English

Javascript:使用XMLHttpRequest发送arrayBuffer

[英]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.

相关问题 JavaScript XMLHttpRequest.responseType as "arraybuffer" -- 如何获取当前的数组缓冲区块 - JavaScript XMLHttpRequest.responseType as "arraybuffer" -- how to get current arraybuffer chunk javafx webview javascript XMLHttpRequest responseType数组缓冲区 - javafx webview javascript XMLHttpRequest responseType arraybuffer 使用 XMLHttpRequest 将变量数据发送到服务器 JavaScript 中的获取方法? - Sending variable data to server using XMLHttpRequest Get method in JavaScript? 使用 javascript XMLHttpRequest 将数据(图像和一些其他参数)发送到 php - Sending data (image and some other parameters) to php by using javascript XMLHttpRequest 使用JavaScript XMLHttpRequest发送凭据不起作用 - Sending credentials with JavaScript XMLHttpRequest is not working 通过JavaScript XHR2对象在PHP中发送ArrayBuffer - Sending an ArrayBuffer in PHP through JavaScript XHR2 object 使用Javascript ArrayBuffer执行XOR的最快方法 - Fastest way to perform XOR using Javascript ArrayBuffer 来自XMLHttpRequest文本的JS ArrayBuffer - JS ArrayBuffer from XMLHttpRequest text 使用JsonP的JavaScript XMLHttpRequest - JavaScript XMLHttpRequest using JsonP 在XmlHttprequest(javascript)中发送生成的(JSZip)zip文件 - Sending generated (JSZip) zip file in XmlHttprequest (javascript)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM