简体   繁体   English

反应js | 当内容类型为多部分/表单数据时如何使边界静态

[英]Reactjs | How to make boundary static when content type is multipart/form-data

We are facing one issue while we are making file upload post call using React-JS, in dev-tools under form data we are getting some browser generated boundary.当我们使用 React-JS 进行文件上传后调用时,我们面临一个问题,在表单数据下的开发工具中,我们得到了一些浏览器生成的边界。

------WebKitFormBoundarypSTl3xdAHAJgTN8A

在此处输入图像描述

And because of this random boundary we are getting issue while we are making call to a third party api.由于这个随机边界,我们在调用第三方 api 时遇到了问题。

Is there any way to make this boundary some fixed value.有什么办法可以使这个边界成为某个固定值。 Something like this :像这样的东西:

----somefixedvalue.

Here is the js code:这是js代码:

function doupload() {
let data = document.getElementById("file").files[0];
console.log('doupload',data);
let formData = new FormData();
formData.append("file", data);
fetch('http://localhost:8081/upload/multipart',
{
method:'POST',
body: formData,
headers: {
   'Content-Type': 'multipart/form-data; boundary=----somefixedboundary'
  }
}).then(res => {
       for(const header of res.headers){
           console.log(`resHeaderName: ${header[0]}, Value:${header[1]}`);
         }
     });
alert('your file has been uploaded');
location.reload();

}; };

can someone help me to solve this?有人可以帮我解决这个问题吗? I'm quite confused here as i have given content type along with some static boundary but no luck.我在这里很困惑,因为我已经给出了内容类型以及一些静态边界,但没有运气。

You can convert a file to the binary string and prepare the body by yourself instead of using FormData as it shown in this answer .您可以将文件转换为二进制字符串并自行准备正文,而不是使用本答案中显示的FormData

But keep in mind that if this ----somefixedvalue substring appears in the file it will be considered a boundary causing body parsing error on receiver side.但请记住,如果这个----somefixedvalue子字符串出现在文件中,它将被视为边界,导致接收方的正文解析错误。 In case of FormData, browser should take care of it and prevent this.在 FormData 的情况下,浏览器应该处理它并防止这种情况发生。

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

相关问题 FormData如何在多部分/表单数据中获取或设置边界 - Angular - FormData how to get or set boundary in multipart/form-data - Angular XMLHttpRequest multipart / form-data:多部分中的边界无效 - XMLHttpRequest multipart/form-data: Invalid boundary in multipart 在XHR中使用multipart / form-data作为Content-Type时获得'400 Bad Request' - Getting '400 Bad Request' when using multipart/form-data as Content-Type in XHR 如何调用$ .post并设置内容类型Multipart / form-data数据 - How call $.post and set content type Multipart/form-data data 如何通过ajax(没有jquery)发送multipart / form-data表单内容? - How to send multipart/form-data form content by ajax (no jquery)? 如何提取通过 multipart/form-data 发送的文件的 Content-Type - How to extract the Content-Type of a file sent via multipart/form-data 如何从multipart / form-data正文请求中删除Content-Type? - How to remove Content-Type from multipart/form-data Body Request? 使用XMLHttprequest上传文件 - 在multipart / form-data中缺少边界 - Uploading a file with XMLHttprequest - Missing boundary in multipart/form-data fetch - multipart/form-data POST 中缺少边界 - fetch - Missing boundary in multipart/form-data POST 如何设置POST的MIME类型-axios中的multipart / form-data? - How to set MIME type for POST - multipart/form-data in axios?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM