简体   繁体   English

Angular Spring 文件上传

[英]Angular Spring file upload

I'm trying to hit the Spring RestController and just getting:我正在尝试点击 Spring RestController 并得到:

osweb.servlet.PageNotFound : Request method 'POST' not supported osweb.servlet.PageNotFound:不支持请求方法“POST”

I believe something is missing to map the controller.我相信映射控制器缺少某些东西。

<div class="col-sm-1" style="background-color:#cccccc;" align="center"><span class="file-input btn btn-primary btn-file">Import file<input type="file" onchange="angular.element(this).scope().uploadScriptFile(this.files)"></input></span></div>


    $scope.uploadCtrFile = function(files) {
        console.log(">>>>>>>>>>>>>>>>uploadCtrFile");
        var fd = new FormData();
        //Take the first selected file
        fd.append("file", files[0]);
        console.log(">>>>>>>>>>>>>>>>uploadCtrFile angular.toJson: "
                + angular.toJson(fd, 2));

        $http.post('/rest/uploadCtrFile/', fd,{
            withCredentials: true,
            headers: {'Content-Type': undefined },
            transformRequest: angular.identity
        }).success(function(fd, status, headers, config) {
            $scope.success = ">>>>>>>>>>>>>>>>uploadCtrFile Success: "+JSON.stringify({data: fd});
            console.log($scope.success);
        })
        .error(function(fd, status, headers, config) {
            $scope.success = ( "failure message: " + JSON.stringify({data: fd}));
            console.log($scope.success);
        });

    };

The controller looks like...控制器看起来像...

            @RequestMapping(value = "/uploadCtrFile/", headers = "'Content-Type': 'multipart/form-data'", method = RequestMethod.POST)
            @ResponseBody
            public void uploadCtrFile(MultipartHttpServletRequest request, HttpServletResponse response) {

                Iterator<String> itr=request.getFileNames();

                MultipartFile file=request.getFile(itr.next());

                String fileName=file.getOriginalFilename();
                log.debug(">>>>>>>>>>>>>>>>>>>submitted uploadCtrFile: "+fileName);
        }

The front end shows these messages...前端显示这些消息...

 ">>>>>>>>>>>>>>>>uploadCtrFile angular.toJson: {}" tl1gen.js:607:0
 "failure message: {"data":     {"timestamp":1457380766467,"status":405,"error":"Method Not Allowed","exception":"org.springframework.web.HttpRequestMethodNotSupportedException","message":"Request method 'POST' not supported","path":"/rest/uploadCtrFile/"}}"

What am I missing ?我错过了什么?

You send undefined as the value of Content-Type , here:您发送undefined作为Content-Type的值,在这里:

headers: {'Content-Type': undefined }

But your controller requires the Content-Type with multipart/form-data value:但是您的控制器需要具有multipart/form-data值的Content-Type

@RequestMapping(..., headers = "'Content-Type': 'multipart/form-data'", ...)

You should either send the correct Content-Type header in your request, like:您应该在请求中发送正确的Content-Type标头,例如:

headers: {'Content-Type': 'multipart/form-data'}

or remove the headers options from your controller definition:或从控制器定义中删除headers选项:

@RequestMapping(value = "/uploadCtrFile/", method = RequestMethod.POST)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM