【发布时间】:2017-03-24 15:04:37
【问题描述】:
我想将@RequestParam 添加到我的http 请求中,以便它与spring MVC @RequestParam 匹配。
如何将其添加到我的文件上传请求中:
/*
@param file - file from input
@param uploadUrl - url
*/
this.uploadFileToUrl = function(file, uploadUrl){
var fd = new FormData();
//add file to FormData
fd.append(file.name, file);
//send request
$http.post(uploadUrl, fd, {
transformRequest: angular.identity,
headers: {'Content-Type': undefined}
})
.success(function(result){
console.log(result);
})
.error(function(err){
console.log(err);
});
}
在我的后端错误是Required String parameter 'filename' is not present
这是我的 Spring MVC controller(只有标题部分):
@Controller
@RequestMapping("/file")
public class FileUploadController {
/**
* Upload single file using Spring Controller
*/
@RequestMapping(value = "/uploadFile", method = RequestMethod.POST)
public @ResponseBody String uploadFileHandler(@RequestParam("filename") String filename, @RequestParam("file") MultipartFile file) {
//rest of the function
}
【问题讨论】:
-
我想您已经查阅了 Angular
$http或FormData的文档,并尝试按照说明添加参数。您遇到了什么问题? -
后端错误 - 所需的字符串参数“文件名”不存在,错误代码 400
-
没关系,我自己想办法,贴出答案。
标签: angularjs spring http spring-mvc