angular - 所需的请求部分“文件”不存在-Java Spring 和 Angular 7
问题描述
我在 Angular 6 中的方法有问题POST
。我想将图像发送到服务器,我从 Postman 尝试过,我的 Spring boot 工作得很好,我的图像保存在服务器上,但是当我从 Angular 项目发送时我有错误:
2019-02-18 10:40:07.086 WARN 6496 --- [nio-8080-exec-5] .wsmsDefaultHandlerExceptionResolver:已解决 [org.springframework.web.multipart.support.MissingServletRequestPartException:所需的请求部分“文件”不存在]
这是我的模板:
<input type="file" (change)="onFileSelectedMethod($event)">
<button (click)="onUploadButton()">Upload!</button>
这是我的组件:
export class AppComponent implements OnInit {
selectedFinenew: File = null;
constructor(private data: DataServiceService, private http: HttpClient) { }
ngOnInit() {}
onFileSelectedMethod(event) {
this.selectedFile = <File>event.target.files[0];
}
onUploadButton() {
const fb = new FormData();
fb.append('image', this.selectedFile, this.selectedFile.name);
this.http.post('api/cateogry/dar/uploadFile', fb).subscribe(res => {
console.log(res);
}
);
}
我在 Spring Boot 中的方法:
@PostMapping("/uploadFile")
public UploadFileResponse uploadFile(@RequestParam("file") MultipartFile file) {
String fileName = fileStorageService.storeFile(file);
String fileDownloadUri = ServletUriComponentsBuilder.fromCurrentContextPath()
.path("/downloadFile/")
.path(fileName)
.toUriString();
return new UploadFileResponse(fileName, fileDownloadUri,
file.getContentType(), file.getSize());
}
解决方案
您需要file
在您的formData
.
你应该改变
fb.append('image', this.selectedFile, this.selectedFile.name);
至
fb.append('file', this.selectedFile, this.selectedFile.name);
或者
改变
@RequestParam("file") MultipartFile file
至
@RequestParam("image") MultipartFile file
推荐阅读
- react-native - Have a Tab Act as a Drawer React Native
- python - 使用 get_dummies 时删除冗余列
- javascript - SAPUI5:主从视图中 sap.ui.table.Table 列的总和
- php - FPDF WriteHtml 不起作用
- ios - 使用 Segue 后如何将数据从一个 ViewController 传递到另一个?
- java - maven-jaxb2-plugin 在同一项目中重用公共 XSD
- visual-studio-code - 如何在*更新之前查看发行说明?
- android - RxJava 同时删除操作
- python - Python DateTime TypeError:无法将类型“Period”与“str”类型进行比较
- mongodb - Springboot2 + reactive + mongo => Tailable 和 Capped 集合