我在项目中使用ng-file-upload。一切都很好,我就像下面一样使用它。
<div class="col-md-4">
<input type="file" ngf-select=""
ngf-change="validateFileInput($files)" ngf-multiple="false"
ngf-allow-dir="false" ng-model="contactGroupForm.files"
id="fileUpload" name="file" required="File must be selected">
</div>
角度控制器,
var paramsData = {'name' : $scope.form.groupName,'description': $scope.form.groupDesc };
Upload.upload({
url: restUrl,
method: "POST",
timeout: 200000,
params : paramsData,
file : $scope.file
}).then(success, error);
Spring rest controller,
@RequestMapping(value = "/rest/save", method = RequestMethod.POST)
public StatusDto saveContactGroup(@RequestParam("file") MultipartFile file,
@RequestParam("contactGroupId") String contactGroupId,
@RequestParam("name") String name,
@RequestParam(value="description", required=false) String description) {
现在,我们计划在表单中提供更多输入。所以,我想通过@RequestBody将所有内容发送到弹簧控制器。
但是,它没有按预期工作,我得到了错误的请求错误。
$scope.formData = {
'name': $scope.contactGroupForm.groupName,
'desc': $scope.contactGroupForm.groupDesc,
'file': $scope.file
};
Upload.upload({
url: restUrl,
method: "POST",
timeout: 200000,
file : data
}).then(success, error);
Spring控制器,
@RequestMapping(value = "/rest/contact/saveContactGroup", method = RequestMethod.POST)
public StatusDto saveContactGroup(@RequestBody ContactForm) {
无论如何,我们可以将表格中的文件和表格数据包装起来发送给Spring休息控制器吗?
由于
答案 0 :(得分:0)
从我的项目
Upload.upload({
url: 'rest/upload',
data: {file: files, 'objectId':$stateParams.objectId}
}).then(function (resp) {
//console.log(resp);
$scope.loadDocList();
}, function (resp) {
// console.log(resp);
}, function (evt) {
$scope.progress = Math.min(100, parseInt(100.0 * evt.loaded / evt.total));
//console.log($scope.progress);
if($scope.progress==100)$scope.progress = 0; //in order to hide progress bar
});
Spring控制器:
@RequestMapping(value={"/upload"}, method = RequestMethod.POST)
@Consumes("multipart/form-data;charset=UTF-8")
@ResponseBody
public Document upload(MultipartHttpServletRequest request /*@RequestBody MultipartFile file*/){
Document resultHolder = new Document();
try {
String objectId = request.getParameter("objectId");
Collection<MultipartFile> files = request.getFileMap().values();
Iterator it = files.iterator();
while(it.hasNext()) {
MultipartFile file = (MultipartFile) it.next();
System.out.println(new String(file.getOriginalFilename().getBytes("iso-8859-1"),"UTF-8"));
docService.upload(file, objectId);
}
resultHolder.put("result", "success");
}catch(Exception ex){
resultHolder.put("result", "fail");
ex.printStackTrace();
}
return resultHolder;
}
将我的Documnt替换为您的StatusDto。 关键点:使用MultipartHttpServletRequest
重要 - 如果你要发送一些UTF-8参数,你应该使用
String param = new String(request.getParameter(“param”)。getBytes(“iso-8859-1”),“UTF-8”);同样适用于request.getOriginalFilename
- 对于英文字母,它可以,但如果文件名包含UTF-8字符,请使用
String filename = new String(file.getOriginalFilename().getBytes("iso-8859-1"),"UTF-8"));