[英]File upload API works in Postman but not React JS
I'm currently developing a Java spring boot and React JS application, and am trying to upload files to the server.我目前正在开发 Java spring boot 和 React JS 应用程序,并尝试将文件上传到服务器。 My spring endpoint looks like this :
我的 spring 端点如下所示:
@PostMapping("/uploadFile")
public UploadFileResponse uploadFile(@RequestParam("file") MultipartFile file) {
My front end code looks like this :我的前端代码如下所示:
let formData = new FormData();
formData.append('file', file);
let xhr = new XMLHttpRequest();
xhr.open("POST", "/api/uploadFile");
xhr.send(formData);
When I submit the request in Postman, with the body selected as form-data, and key of file, everything works and my file is uploaded to the server.当我在 Postman 中提交请求时,正文选择为表单数据和文件键,一切正常,我的文件上传到服务器。
However, when I try the same thing using the front end code, the server says:但是,当我使用前端代码尝试相同的事情时,服务器说:
Required request part 'file' is not present
I've tried searching online, and it seems like I'm doing everything correct.我试过在网上搜索,似乎我做的一切都是正确的。 I know that my server side code is correct as the request works with Postman.
我知道我的服务器端代码是正确的,因为该请求适用于 Postman。
Any insight is appreciated!任何见解表示赞赏!
如果XMLHttpRequest
出现在那里,请尝试从XMLHttpRequest
完全删除标头,或者将标头保留为没有Content-Type
属性的空对象{}
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.