简体   繁体   English

将自定义标题添加到 Content-disposition 旁边的 FormData/multipart

[英]Add Custom Header to the FormData/multipart next to Content-disposition

Sending images to the server through multipartform-data with additional headers for each image.通过 multipartform-data 将图像发送到服务器,每个图像都有额外的标题。

I want to add lang and url custom headers.我想添加langurl自定义标头。 Each image needs to have personal values in these headers.每个图像都需要在这些标题中具有个人值。

Here is an example:下面是一个例子:

--B_kpXDt398Po5ytQzY0P6SBEM6VmzrsPAAINKJgt
lang: en
url: http://example.com
Content-Disposition: form-data; name="images"; filename="src%5Ctest%5Cresources%5CtestFile.jpeg"
Content-Type: image/jpeg

--B_kpXDt398Po5ytQzY0P6SBEM6VmzrsPAAINKJgt
lang: ru
url: http://anotherExample.com
Content-Disposition: form-data; name="images"; filename="src%5Ctest%5Cresources%5CtestFile.jpeg"
Content-Type: image/jpeg

My current code:我目前的代码:

let formData = new FormData();

let options = {
    header: {
      lang: 'en',
      url: 'example.com',
    }
};

// I also tried using these options:
//  let options = {
//      header: '\r\n lang: ru \r\n url: example.com'
//    };

formData.append("images", myImageBlob, options);

I post form with Axios and got this:我用 Axios 发布表单并得到这个:

------WebKitFormBoundaryqOIB7duTkYj0twQA
Content-Disposition: form-data; name="images"; filename="[object Object]"
Content-Type: image/png

Does anyone have any ideas or clues how to add additional custom headers to formData?有没有人有任何想法或线索如何向 formData 添加额外的自定义标头?

Appreaciate any suggestions欣赏任何建议

One approach for this use case is to have extra meta data form post fields for lang and url此用例的一种方法是为langurl设置额外的元数据表单 post 字段

An example of the form:表格示例:

<form>
    
    <input type="file" name="images[]" multiple>
    <!--for first image-->
    <input type="hidden" name="images_lang[]" value="en">
    <input type="hidden" name="images_url[]" value="example.com">

    <!--for second image-->
    <input type="hidden" name="images_lang[]" value="ru">
    <input type="hidden" name="images_url[]" value="anotherexample.com">
</form>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM