2016-11-10 122 views
0

我想添加@RequestParam到我的http请求,所以它会匹配spring MVC @RequestParamAngular - 文件上传请求,添加@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 

}

+0

我认为你已经咨询了Angular'$ http'或'FormData'的文档,并试图按照描述添加参数。你遇到过什么问题? – zeroflagL

+0

后台错误 - 必需的字符串参数'文件名'不存在,错误代码400 –

+0

没关系,我想出了自己,发布了答案。 –

回答

2

所以我只是追加了另一个参数给我的FormData

fd.append('file', file); 
fd.append('filename', file.name); 

匹配@RequestParam的。

谢谢。

相关问题