"当前请求不是多部分请求" angularjs和弹簧靴

时间:2017-05-01 19:59:35

标签: java angularjs spring spring-boot

我知道这个问题一直被问到,但是在尝试了所有可能的选项之后,我仍然遇到了这个错误:

the current request is not a multipart

这是我的角度代码:

app.controller("controller", function ($scope, $http, $window, $log) {

    $scope.upload = function () {
        var file = $scope.myFile;
        var fd = new FormData();
        fd.append('file', file);
        $http.post('upload', fd, {
            transformRequest: angular.identity,

        })
            .success(function(){
            })
            .error(function(){
            });
    };

控制器代码:

@RequestMapping(value = "/upload", method = RequestMethod.POST)
    public Callable<FileUpload> uploadFile(@RequestParam("file") MultipartFile file) throws Exception {
        parser = fileHandlerService.getParser(file);
        Callable<FileUpload> asyncResult = new Callable<FileUpload>() {
            @Override
            public FileUpload call() throws Exception {
                FileUpload fileUpload = fileHandlerService.getUploadStatus();
                return fileUpload;
            }
        };

        return asyncResult;
    }

HTML code:

<div class="vertical-center">
        <input type="file" file-model="myFile">
        <button ng-click="upload()" class="btn btn-primary btn-lg">Upload
        </button>
</div>

我尝试添加:headers: {'Content-Type': undefined }但是添加标题后,我不会再收到错误500但我收到错误400.

我通过Postman发送文件,没有任何标题,它工作正常。所以我不明白为什么它没有通过棱角分明。

提前致谢。

1 个答案:

答案 0 :(得分:1)

除了添加标题:{'Content-Type':undefined}之外,您还可以将div更改为表单并将enctype =“multipart / form-data”添加到表单中。你的代码看起来像;

<form class="vertical-center" enctype="multipart/form-data">
    <input type="file" file-model="myFile">
    <button ng-click="upload()" class="btn btn-primary btn-lg">Upload
    </button>
</form>