I'm try to upload file with angularjs, but there has a problem is "The current request is not a multipart request" and I almost try every solution from google but not solve my problem, I hope someone can answer my question, THANKS.
this is my springMVC config
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="defaultEncoding" value="UTF-8"></property>
<property name="maxUploadSize" value="5242880"></property>
</bean>
this is html
<input type="file" id="file"/>
<button class="btn btn-primary" type="button" ng-click="uploadFile()">上传</button>
this is angular controller
$scope.uploadFile = function () {
uploadService.uploadFile().success(function (response) {
if(response.success){
$scope.image_entity.url = response.message;
}else{
alert(response.message);
}
})
}
this is angular service
this.uploadFile = function () {
var formData = new FormData();
formData.append("file", file.files[0]); //文件上传框的name
return $http({
url: "/upload.do",
method: "post",
data: formData,
headers: {"Content-Type": undefined},
transformRequest: angular.identity
})
}
this is uploadController
@RequestMapping("/upload")
public ReturnResult upload(MultipartFile file){
String fullName = file.getOriginalFilename();
String extName = fullName.substring(fullName.lastIndexOf(".") + 1);
try {
FastDFSClient client = new FastDFSClient("classpath:config/fdfs_client.conf");
String fileId = client.uploadFile(file.getBytes(), extName);
String url = file_server_url + fileId;
return new ReturnResult(true, url);
} catch (Exception e) {
e.printStackTrace();
}
}
I see the google browser send
Request Method: POST
Request Headers
Content-Type: multipart/form-data; boundary=----WebKitFormBoundaryrzP8MUha8lcbDzdn
Form Data
------WebKitFormBoundaryrzP8MUha8lcbDzdn
Content-Disposition: form-data; name="file"; filename="1.jpg"
Content-Type: image/jpeg
------WebKitFormBoundaryrzP8MUha8lcbDzdn--
is that have problem? please help me.