【问题标题】:Angular - file upload request, adding @RequestParamAngular - 文件上传请求,添加@RequestParam
【发布时间】: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 $httpFormData 的文档,并尝试按照说明添加参数。您遇到了什么问题?
  • 后端错误 - 所需的字符串参数“文件名”不存在,错误代码 400
  • 没关系,我自己想办法,贴出答案。

标签: angularjs spring http spring-mvc


【解决方案1】:

所以我只是将另一个参数附加到我的 FormData:

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

匹配@RequestParam

谢谢。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-10-11
    • 2021-08-10
    • 2019-05-20
    • 2017-01-06
    • 2021-07-03
    • 1970-01-01
    • 2019-05-12
    相关资源
    最近更新 更多