[英]Required MultipartFile parameter 'file' is not present when I request from Angular 4
I can do a request uploading a file from Postman, but when I do the request from Angular, it is returned the WARN "Required MultipartFile parameter 'file' is not present" 我可以从Postman上载文件,但从Angular发出请求时,它会返回警告:“不存在所需的MultipartFile参数'file'”
It follows my resource in API. 它遵循我在API中的资源。
@PostMapping
public ResponseEntity<Conteudo> publicaConteudo(@RequestParam("file") MultipartFile file) throws JsonParseException, JsonMappingException, IOException {
/* ANYTHING */
return ResponseEntity.ok(new Conteudo());
}
And my service in Angular. 还有我在Angular的服务。 I'm using JWT, but I also trying to do the request using HttpClient.
我正在使用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();
}
And how I can do in Postman 以及我在邮递员中的工作方式
I tried many solutions that I found, as create a Bean MultipartResolver and others, but any solve my problem and what I think more interesting is that it works fine in Postman. 我尝试了许多发现的解决方案,例如创建Bean MultipartResolver和其他解决方案,但是任何解决了我的问题的方法,我认为更有趣的是,它在Postman中可以正常工作。
In your AbstractAnnotationConfigDispatcherServletInitializer class do you have this method?: 在您的AbstractAnnotationConfigDispatcherServletInitializer类中,您是否具有此方法?
@Override
protected Filter[] getServletFilters() {
CharacterEncodingFilter encodingFilter = new CharacterEncodingFilter("UTF-8");
encodingFilter.setForceEncoding(true);
return new Filter[]{new DelegatingFilterProxy("springSecurityFilterChain"),
new OpenEntityManagerInViewFilter(),encodingFilter, new MultipartFilter()};
}
maybe you haven´t registered the multipart filter. 也许您尚未注册多部分过滤器。 Hope this help.
希望对您有所帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.