我得到的是 "所需的请求部分'文件'不存在。"它在postman中工作得很好,但在react js中没有。我试图在Spring boot restfull api中调用一个端点。
按照代码。
function sendUploadFileRequest(file, userName, caseRef) {
/* var formData = {
username: userName,
caseRef: caseRef,
file: file
}; */
let formData = new FormData();
formData.append('file', file);
const requestOptions = putRequestWithBodyUploadFile(formData)
console.log(requestOptions);
return fetch(
`${config.apiUrl}/distribution-cases/uploadFileToSharepoint?access_token=${authHeader().Authorization}`,
requestOptions
).then(handleResponse);
}
export function putRequestWithBodyUploadFile(body) {
return {
method: 'POST',
headers: {
'Content-Type': 'multipart/form-data; boundary=AaB03x',
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Headers': 'Origin, X-Requested-With, Content-Type',
'Access-Control-Request-Method': '*'
},
body: JSON.stringify(body)
}
}
@PostMapping(value = "/distribution-cases/uploadFileToSharepoint", consumes = "multipart/form-data")
@ApiOperation(value = "Upload a file to sharepoint. This api will upload a file to Sharepoint for the associated user. Once the file has been uploaded an email will be sent to the case owner. ", httpMethod = "PUT")
public ResponseEntity<StringBuilder> uploadFileToSharepoint(@RequestParam("file") MultipartFile file) throws Exception {
StringBuilder errorMessage = new StringBuilder();
}
我的问题是,我将dataForm转换为json的时候,使用了 JSON.stringify() 在请求的正文中。而且它是 不 所需的头部内容= multipartform-data,因为浏览器会自动处理它。