Unable to upload file from Angular client to Spring Java server: Says 400 error

自古美人都是妖i 提交于 2020-01-06 19:24:19

问题


When I am uploading a file from Postman rest client to the server(spring application deployed on a remote machine), I am able to upload the file without any issue.

But when I try to write a rest client in angular.js, and send over the request, I get 400 Bad Request Error. I know it's because of some syntax issue between what is send from client and what server is expecting.

Server side code:

@CrossOrigin(origins = "*") @RequestMapping(value="/upload", method=RequestMethod.POST, consumes = {"multipart/form-data"})

public @ResponseBody String handleFileUpload(@RequestParam("files") MultipartFile file){ . . . . }

Client side code:

$scope.uploadFiles = function () {
    alert("inside");

    var request = {
        method: 'POST',
        url: 'http://IP ADDRESS and PORT NUMBER/upload',
        data: formdata,
        headers: {
            'Content-Type': undefined
        }
    };


    $http(request)
        .success(function (response) {
            alert("success: "+response);

        })
        .error(function (err) {alert("error: "+err);
        });
}

回答1:


I have uploaded the code here for file upload using Spring and AngularJS:

https://gist.github.com/abdulrafique/9219f7164fdf5dc6dfa8da110be6a04e



来源:https://stackoverflow.com/questions/37847652/unable-to-upload-file-from-angular-client-to-spring-java-server-says-400-error

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!