【发布时间】:2019-08-23 09:09:46
【问题描述】:
我正在尝试使用 Angular 4 上传 csv 文件。
这是我在 Spring Boot 中的控制器 -
@PostMapping("/sop-master/csv-upload")
@ResponseStatus(HttpStatus.OK)
@ResponseBody
public List<SopMasterDto> uploadCSV(@RequestParam("file") MultipartFile file, @RequestBody SopMasterDto sopMasterDto) throws IllegalStateException, IOException {
return convertToDtos(sopMasterService.updateSopMaster(file, sopMasterDto.getUsername(),sopMasterDto.getPassword()), SopMasterDto.class);
}
这就是我使用 Angular 4 在 http post 中发送 csv 文件的方式 -
uploadCSV(file: File) {
let formData:FormData = new FormData();
formData.append('file', file, file.name);
let headers = new Headers();
headers.append('Content-Type', 'multipart/form-data; boundary=Inflow');
headers.set('Accept', 'application/json');
let options = new RequestOptions({ 'headers': headers })
return this.http.post(AppSettings.API_ENDPOINT + 'manage/sop-master/csv-upload/', formData, options)
.map((res) => console.log(res))
.catch(error => Observable.throw(error));
}
这就是我的看法-
<input type="file" class="btn btn-default" accept=".csv" (change)="changeListener($event.target.files)">
changeListener (files: FileList) {
if (files && files.length > 0) {
let file: File = files.item(0);
this.manageService.uploadCSV(file)
.subscribe((response) => {
this.refreshDatatable();
});
}
}
因此,通过 Postman 完成文件上传工作绝对正常,但通过 Angular 4 完成时我不断收到此错误 -
"status":400, "error":"Bad Request","exception":"org.springframework.web.multipart.support.MissingServletRequestPartException", "message":"Required request part 'file' is not present"
更新:
附上我的浏览器控制台上出现的错误截图。
请求标头
从请求中删除自定义边界后的请求标头
从标头中删除内容类型后的请求标头
【问题讨论】:
-
检查实际请求以了解发生了什么。
-
检查浏览器控制台以查看实际请求。这是尽可能具体的。
-
那不是请求,而是响应——更准确地说,是应用程序对它的反应。显示请求。
-
我已经添加了请求。
标签: angular spring-boot