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