简体   繁体   English

文件上传 API 在 Postman 中有效,但在 React JS 中无效

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

相关问题 使用 postman 测试上传文件 api - testing upload file api with postman 无法将文件或图像从 Postman 上传到 Java API - Unable to upload file or Image from Postman to Java API 使用 JS Fetch API 上传时负载太大错误 (413)。 通过邮递员工作正常 - Payload too large error (413) when uploading using JS Fetch API. Works fine via Postman 在邮递员 api 中工作正常,但在 java 中却没有 - In postman api works fine but in java it doesn't 使用Postman和Spring的多部分文件上传:对象不是声明类的实例 - Multipart file upload using Postman and Spring : Object is not an instance of declaring class 使用POSTMAN将文件上传到Spring MVC 3.0.x - upload file using POSTMAN to Spring MVC 3.0.x 文件上传适用于 Postman 但不适用于从浏览器运行的 Javascript - File Upload is working with Postman but not with Javascript running from browser 如何将文件编码为base64,然后使用JS将其作为multipart文件上传到后端api? - How do you encode a file to base64 then upload as multipart file to backend api using JS? 尝试用邮递员上传MultipartFile - Trying to upload MultipartFile with postman 如何捕获其余api控制器上的文件以及使用Postman发送它的方法 - how to catch a file on the rest api controller and what method to send it with Postman
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM