我正在尝试将最近使用Angular2下载的文件上传到Spring API Rest。
问题是(在春季应用上显示)...
该请求被拒绝,因为未找到多部分边界
在org.apache.tomcat.util.http.fileupload.FileUploadBase $ FileItemIteratorImpl。(FileUploadBase.java:831) 〜[tomcat-embed-core-8.5.28.jar:8.5.28]在 org.apache.tomcat.util.http.fileupload.FileUploadBase.getItemIterator(FileUploadBase.java:256) 〜[tomcat-embed-core-8.5.28.jar:8.5.28]在 org.apache.tomcat.util.http.fileupload.FileUploadBase.parseRequest(FileUploadBase.java:280) 〜[tomcat-embed-core-8.5.28.jar:8.5.28]在 org.apache.catalina.connector.Request.parseParts(Request.java:2869) 〜[tomcat-embed-core-8.5.28.jar:8.5.28]在 org.apache.catalina.connector.Request.parseParameters(Request.java:3216) 〜[tomcat-embed-core-8.5.28.jar:8.5.28]在 org.apache.catalina.connector.Request.getParameter(Request.java:1137) 〜[tomcat-embed-core-8.5.28.jar:8.5.28]
在客户端,以“ multipart / form-data”作为内容类型发送请求。
我该如何解决?
fileDownloaderService
upload(file) {
const formData = new FormData();
formData.append('file', file);
const req = new HttpRequest('POST', this.urlUpload, file, {
headers: new HttpHeaders({'Content-Type':'multipart/form-data'}),
reportProgress: true
});
return this.http.request(req);
}
app.component
upload() {
let file = this.generate_dummy_file(50000000);
this.downloader.upload(file).subscribe( event => {
if (event.type === HttpEventType.UploadProgress) {
} else if (event instanceof HttpResponse) {
console.log('File is completly uploaded!');
}
});
}
generate_dummy_file(size) {
return new Blob([new ArrayBuffer(size)], {type: 'multipart/form-data'});
};
和弹簧边
@PostMapping("/uploadFile")
public UploadFileResponse uploadFile(@RequestParam("file") MultipartFile file) {
return ...;
}
感谢您的帮助
答案 0 :(得分:1)
尝试一下,
const formData = new FormData();
formData.append("file", file);
formData.append("reportProgress", true);
使用httpclient,
return this.httpclient.post(this.urlUpload, formData);
答案 1 :(得分:0)
您实际上是在向服务器发送df_comp = df_matrix.join(df_host, how='outer', lsuffix='_matrix', rsuffix='_host')
吗?
尝试将文件包装在FormData
对象中,以FormData
自动添加多部分边界。
HttpClient