【发布时间】:2018-01-31 01:20:46
【问题描述】:
我可以从 Postman 发出上传文件的请求,但是当我从 Angular 发出请求时,它会返回 WARN“必需的 MultipartFile 参数‘文件’不存在”
它遵循我在 API 中的资源。
@PostMapping
public ResponseEntity<Conteudo> publicaConteudo(@RequestParam("file") MultipartFile file) throws JsonParseException, JsonMappingException, IOException {
/* ANYTHING */
return ResponseEntity.ok(new Conteudo());
}
还有我在 Angular 中的服务。我正在使用 JWT,但我也尝试使用 HttpClient 执行请求。
upload(file: File, conteudo: Conteudo): Promise<any> {
let formData: FormData = new FormData();
formData.append('file', file, file.name);
/* USING JWT
return this.http.post(this.conteudoUrl, formData)
.toPromise()
.then(response => response.json());
*/
let h1 = new HttpHeaders().set('Authorization', 'Bearer ' + localStorage.getItem('token'));
const req = new HttpRequest('POST',this.conteudoUrl, formData, {
headers: h1
});
return this.httpClient.request(req).toPromise();
}
我在 Postman 中的表现如何
我尝试了许多我发现的解决方案,例如创建 Bean MultipartResolver 和其他解决方案,但任何解决我的问题的方法都可以解决,我认为更有趣的是它在 Postman 中运行良好。
【问题讨论】:
标签: java spring angular spring-boot angular2-jwt