【发布时间】:2020-04-19 23:01:38
【问题描述】:
文件上传.html
<p-fileUpload name="file" customUpload="true" (uploadHandler)="myUploader($event)"
type="file" accept=".lsx, .xlsx" [auto]="true" ></p-fileUpload>
文件上传.ts
myUploader(event):void{
const formdata:FormData = new FormData();
const fileToUpload: File = event.files[0];
formdata.append('file', fileToUpload);
const req = new HttpRequest('POST', 'http://localhost:8081/fileUpload', formdata);
this.httpClient.request(req).subscribe();
}
文件上传.java
@PostMapping(value = "/fileUpload")
public ApiResponse giftCardBatchList(@RequestParam("file") MultipartFile file) {
fileUploadService.upload(file);
return new ApiResponse(ApiResponseStatus.SUCCESS, null);
}
控制器没有捕捉到请求,我得到了这个异常
MissingServletRequestPartException:所需的请求部分“文件”是 不存在
如果我改变了
@RequestParam("file") MultipartFile 文件
到
@RequestParam("file") MultipartFile[] 文件
控制器捕获请求,但文件数组不包含任何内容。它是空的:(
有什么想法吗?
谢谢你们!
【问题讨论】:
-
试试这种方式
@RequestParam MultipartFile file告诉我。 -
得到了同样的异常“所需的请求部分'文件'不存在”:(
-
尝试在请求的标头中传递内容类型,像这样。
new HttpRequest('POST', 'http://localhost:8081/fileUpload', formData, {headers: headers});参考这个stackoverflow.com/questions/48279484/… -
我用相同的标头做了完全相同的请求,但得到了相同的异常:(
标签: java angular spring upload