简体   繁体   English

“当前请求不是多部分请求” angularjs和spring boot

[英]“the current request is not a multipart request” angularjs and spring boot

I know this question has been asked over and over, however after trying all the possible options still I get this error: 我知道这个问题已经被一遍又一遍地询问,但是在尝试了所有可能的选项后,我仍然收到此错误:

the current request is not a multipart

Here is my code in angular: 这是我的角度代码:

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(){
            });
    };

Controller code: 控制器代码:

@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: HTML代码:

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

I tried putting: headers: {'Content-Type': undefined } however with adding header, I don't get error 500 anymore but I get error 400. 我尝试放置: headers: {'Content-Type': undefined }但是,添加标头后,我不再收到错误500,但收到错误400。

I send the file through Postman without any headers, and it works fine. 我通过没有任何标题的Postman发送文件,并且工作正常。 So I don't understand why it doesn't work through angular. 所以我不明白为什么它不能通过角度工作。

Thanks in advance. 提前致谢。

In addition to adding headers: {'Content-Type': undefined } , you can change the div to a form and add enctype="multipart/form-data" to the form. 除了添加标头:{'Content-Type':undefined}之外,您还可以将div更改为表单,并将enctype =“ multipart / form-data”添加到表单。 ie your code would look like; 即您的代码看起来像;

<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>

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

相关问题 当前请求不是多部分请求:Spring boot - Current request is not a multipart request : Spring boot Spring 启动:当前请求不是多部分请求 - Spring boot: Current request is not a multipart request Spring Boot + Angular - MultipartException:当前请求不是多部分请求 - Spring Boot + Angular - MultipartException: Current request is not a multipart request Spring-boot rest api当前请求不是邮递员的多部分请求 - Spring-boot rest api current request is not multipart request with postman 使用AngularJS的Java Spring Boot多部分POST请求 - Java Spring Boot multipart POST request using AngularJS Spring 引导多部分 REST 端点在从 Postman 调用时给出“当前请求不是多部分请求” - Spring Boot Multipart REST endpoint gives 'Current request is not a multipart request' when called from Postman org.springframework.web.multipart.MultipartException:当前请求不是多部分请求 spring boot - org.springframework.web.multipart.MultipartException: Current request is not a multipart request spring boot Spring / Jboss-当前请求不是多部分请求 - Spring/Jboss - Current Request is not multipart request 当前请求不是多部分请求 - Spring MVC - Current request is not a multipart request - Spring MVC 当前请求不是多部分请求 Spring Boot 和 Postman (Uploading json file plus extra field) - Current request is not a multipart request Spring Boot and Postman (Uploading json file plus extra field)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM