[英]JS FormData sends file as application/ocet-stream, how to set it as multipart/form-data?
As in the title, I'm trying to upload file to Spring Rest Controller using:如标题所示,我正在尝试使用以下方法将文件上传到 Spring Rest Controller:
handleOnChange() {
let inputFile = document.getElementById("file-input");
this.file.file = inputFile.files[0];
this.file.filename = inputFile.value;
}
render() {
return (
<div className="file-input">
<input id="file-input" type="file"
onChange={(event) => this.handleOnChange()} />
</div>
);
}
upload() {
let formData = new FormData();
formData.append("file", this.data.file.file, "/D:/temp/test2.xlf");
formData.append("request", JSON.stringify(request));
let reqOptions = {
method: 'POST',
body: formData
}
fetch('http://127.0.0.1:8081/multi-mt-evaluation/report', reqOptions)
.then(response => response.json())
.then(data => console.log(data));
}
But when fetch is called the file is sent as application/octet-stream, is there a simple, or more complicated way to set it as multipart/form-data?但是当 fetch 被调用时,文件作为 application/octet-stream 发送,是否有一种简单或更复杂的方法将其设置为 multipart/form-data?
Try specifying the content type by adding it to your request headers:尝试通过将其添加到您的请求标头来指定内容类型:
let reqOptions = {
method: 'POST',
body: formData,
headers: {
'Content-Type': 'multipart/form-data',
},
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.