I am trying to upload a file using angularjs and spring MVC
I have a multipartResolver bean in application-context.xml.
<mvc:annotation-driven />
<bean id="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="maxUploadSize" value="2097152" />
</bean>
my form look like this:
<form method="post" id="fromFileUpload" enctype="multipart/form-data"
ng-submit="continueFileUpload()">
<div class="form-group">
<label class="control-label col-sm-4 col-xs-12" for="quoteIdentifier">Quote Identifier : </label>
<div class="col-xs-4 input-max">
<select type="text" class="form-control " name="quoteIdentifier" id="quoteIdentifier" ng-model="quoteIdentifier" ng-options="">
<option style="display: none" value="">Choose</option>
</select>
</div>
</div>
<div class="form-group">
<label class="control-label col-sm-4 col-xs-12" for="file">Please upload the file : <span class="required">*</span> </label>
<div class="col-xs-4 input-max controls ">
<input class="inline-block" type="file" name="file" ng-model="file" data-rule-required="true" id="file" accept=".csv,.xsl,.xml,.mpp,application/vnd.ms-excel">
</div>
<span id="vaildFile" class="text-success icon-ok hide">Valid File</span> <span id="invaildFile" class="text-error icon-remove hide"> Invalid File</span>
</div>
<div class="box-header">
<div class="actions">
<button type="submit" class="btn btn-primary">
<i class="icon-arrow-right"></i> Continue
</button>
</div>
</div>
</form>
$scope.continueFileUpload=function (){
var uploadUrl=serverUrl+"continueFileUpload";
var formData=new FormData();
formData.append("file",file.files[0]);
$http({
method: 'POST',
url: uploadUrl,
headers: {'Content-Type': false},
data: formData,
transformRequest: function(data, headersGetterFunction) {
return data;
}
})
.success(function(data, status) {
alert("success");
})
};
spring controller:
@Controller
public class FileUploadController {
@RequestMapping(value = "/continueFileUpload", method = RequestMethod.POST)
@ResponseBody
public String continueFileUpload(HttpServletRequest request,
HttpServletResponse response){
MultipartHttpServletRequest mRequest;
try {
mRequest = (MultipartHttpServletRequest) request;
mRequest.getParameterMap();
Iterator<String> itr = mRequest.getFileNames();
while (itr.hasNext()) {
MultipartFile mFile = mRequest.getFile(itr.next());
String fileName = mFile.getOriginalFilename();
System.out.println(fileName);
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
When I add multipart/form-data for the header I get **the request was rejected because no multipart boundary was found**
exceptions
If I didnt add I get org.apache.catalina.connector.RequestFacade cannot be cast to org.springframework.web.multipart.MultipartHttpServletRequest
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…