Spring + Angular无法两次上传同一文件
无法上传同一文件两次.如果上传其他文件,则可以正常工作
Unable to upload same file twice. If uploading different files its working
Chrome中的网络下的错误
Error under Network in chrome
{ timeStamp: ......, status: 417
error: 'Bad Request',
message: 'Required request part 'file' is not present'
path: 'url as hosted on Tomcat'
}
Spring Boot Controller.java文件
Spring Boot Controller.java file
@PostMapping("/Post")
public ResponseEntity<String> handleFileUpload(@RequestParam("file")
MultipartFile file){ String Message=""; try .......(and so on)}
我的Angular组件
My Angular Component
<form [formGroup]="uploadForm" (ngSubmit) = "onSubmit()">
<input type="file" id="selectFile" formControlName="file1" name="selectFile"
(change)="fileEvent($event)"/>
<input type="submit" name="Submit"/>
</form>
Component.ts文件
Component.ts file
fileEvent(e) {
this.data = e.target.files[0];
}
onSubmit() {
let headers: any = new Headers();
headers.append('Content-type', 'undefined');
let formData = new FormData();
formData.append("file", this.data);
const req5 = new HttpRequest('POST', 'url as hosted on TOMCAT', formData,
reportProgress: true,
responseType: 'text'
});
return this.httpClient.request(req5).subscribe(e => {(
console.log(e);
)}
}
我在哪里弄错了?
您的问题听起来像是浏览器缓存,即第一次处理请求,第二次处理不同.如果这是问题的根源,那么您可以尝试在POST URL的末尾附加一个随机查询参数,例如
Your problem sounds like there is browser caching, whereby the first time the request goes through, and the second time something different happens. If this be the source of the problem, then you may try appending a random query parameter to the end of the POST URL, e.g.
var url = 'url as hosted on TOMCAT';
url = url + (new Date()).getTime();
是的,将查询参数绑定到POST请求似乎很奇怪,但是应该没有什么阻止您执行此操作.理想情况下,这足以禁用浏览器缓存.
Yes, it may seem strange to bind a query parameter to a POST request, but there should be nothing preventing you from doing this. Ideally, this would be enough to disable browser caching.