0
我想添加@RequestParam
到我的http
请求,所以它会匹配spring MVC @RequestParam
。Angular - 文件上传请求,添加@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'的文档,并试图按照描述添加参数。你遇到过什么问题? – zeroflagL
后台错误 - 必需的字符串参数'文件名'不存在,错误代码400 –
没关系,我想出了自己,发布了答案。 –