【发布时间】:2019-07-11 16:13:53
【问题描述】:
我在 Angular 6 中的 POST 方法有问题。我想将图像发送到服务器,我从 Postman 尝试过,我的 Spring boot 工作得很好,我的图像保存在服务器上但是当我从 Angular 项目发送我有错误:
2019-02-18 10:40:07.086 警告 6496 --- [nio-8080-exec-5] .w.s.m.s.DefaultHandlerExceptionResolver :已解决 [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());
}
【问题讨论】:
-
你能分享你的
ControllerJava 代码吗? -
是的,现在你可以看到了。
-
你找到解决办法了吗?
标签: angular spring-boot