Rest 将文件从angular 6应用程序发送到spring boot web api;所需请求部分';文件';“不存在”;
我正在尝试将一个文件从angular 6前端发送到spring boot web API。 但它给了我以下的错误Rest 将文件从angular 6应用程序发送到spring boot web api;所需请求部分';文件';“不存在”;,rest,spring-boot,file-upload,multipartform-data,angular6,Rest,Spring Boot,File Upload,Multipartform Data,Angular6,我正在尝试将一个文件从angular 6前端发送到spring boot web API。 但它给了我以下的错误 Bad Request","message":"Required request part 'file' is not present 这是我上传文件的html代码 <div class="form-group"> <label for="exampleInputFile">File input</label> <input
Bad Request","message":"Required request part 'file' is not present
这是我上传文件的html代码
<div class="form-group">
<label for="exampleInputFile">File input</label>
<input type="file" name="file" (change)="fileChange($event)" class="form-control-file" id="exampleInputFile" aria-describedby="fileHelp">
</div>
<button (click)="uploadFile()" type="button" class="btn btn-primary">Upload</button>
这是角度衬线
uploadFIle(formData:FormData){
let headers = new Headers();
headers.append('Accept', 'application/json');
headers.append("Content-Type", 'multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW');
return this.http.post(this.url+'upload',formData,{headers: headers})
};
这是后端控制器
@CrossOrigin(origins = "*")
@PostMapping(value = "api/upload")
public String uploadReviews(@RequestParam("file") MultipartFile file, RedirectAttributes redirectAttributes) {
if (file.isEmpty()) {
return null;
}
try {
byte[] bytes = file.getBytes();
Path path = Paths.get(uploadFolder + file.getOriginalFilename());
uploadFile = path.toString();
Files.write(path, bytes);
sessionID = "6";
} catch (IOException e) {
e.printStackTrace();
return null;
return sessionID;
}
上面的API服务可以完美地处理邮递员请求。但不能处理角度要求
有人能帮我吗?而不是这个:-
return this.http.post(this.url+'upload',formData,{headers: headers})
使用以下命令:-
return this.http.post(this.url+'upload',{"file" : formData},{headers: headers})
希望这对我有帮助
downloadPdf(file: File): Observable<HttpEvent<any>>{
const formData: FormData = new FormData();
formData.append('file', file);
const req = new HttpRequest('POST', `${this.url}/upload`, formData, {
reportProgress: true,
responseType: 'arraybuffer' as 'json'
});
return this.http.request(req);
}
下载PDF(文件:file):可观察{
const formData:formData=new formData();
formData.append('file',file);
const req=new HttpRequest('POST','${this.url}/upload',formData{
报告进展:是的,
responseType:'arraybuffer'作为'json'
});
返回这个.http.request(req);
}
您找到解决方案了吗?
downloadPdf(file: File): Observable<HttpEvent<any>>{
const formData: FormData = new FormData();
formData.append('file', file);
const req = new HttpRequest('POST', `${this.url}/upload`, formData, {
reportProgress: true,
responseType: 'arraybuffer' as 'json'
});
return this.http.request(req);
}