Answer the question
In order to leave comments, you need to log in
Uploading a file via Spring?
I have a dev server running angular 2 at localhost:4200 and spring tomcat at localhost:8080.
I'm trying to upload a file to the server in the following way:
Angular code:
uploadAvatar(file:File){
let xhr = new XMLHttpRequest()
xhr.open("POST",`http://localhost:8080/api/upload/avatar`)
xhr.setRequestHeader("Content-Type","multipart/form-data")
xhr.send(file)
}
@RequestMapping(value = "/api/upload/avatar", method = RequestMethod.POST)
public String uploadFile(MultipartFile file){
log.info(file);
return file.getName();
}
org.springframework.web.multipart.MultipartException: Could not parse multipart servlet request;
nested exception is java.io.IOException: org.apache.tomcat.util.http.fileupload.FileUploadException: the request was rejected because no multipart boundary was found
Answer the question
In order to leave comments, you need to log in
Added two bins and everything worked.
@Bean(name = "commonsMultipartResolver")
public MultipartResolver multipartResolver() {
return new StandardServletMultipartResolver();
}
@Bean
public MultipartConfigElement multipartConfigElement() {
MultipartConfigFactory factory = new MultipartConfigFactory();
factory.setMaxFileSize("10MB");
factory.setMaxRequestSize("10MB");
return factory.createMultipartConfig();
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question