简体   繁体   中英

Required MultipartFile parameter 'file' is not present in spring 4.3.1

I am using spring 4.3.1 and I am going to upload file using ng-file-upload library. this is my javascript code and when I connect the javascript code to php server, it works well.

        var promise = Upload.upload({
            url: url + "upload",
            method: 'POST',
            file: file,
            ignoreLoadingBar: true
        }).success(function(response) {
            flatForm.jsonForm = response.jsonForm;
            flatForm.xmlForm = response.xmlForm;
        }).error(function(response) {
            $rootScope.$broadcast('veil:hide', {});
        });

And I appended commons-io-2.4.0.0.jar and commons-fileupload-1.3.1.jar in /web-inf/lib folder. e And I added multipartResolver in applicationContext.xml file.

 <bean id="multipartResolver"
    class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
    <property name="maxUploadSize" value="1000000000" />
</bean>

And this is my controller class.

    @ResponseBody
@RequestMapping(value = "/upload", method = RequestMethod.POST)
public void upload(@RequestParam("file") MultipartFile file) throws Exception {
    if (file == null || file.isEmpty()) {
        throw new Exception("No file was sent.");
    }
}

But when I upload a file, I get such error.

Required MultipartFile parameter 'file' is not present

How can I fix this? Please help me. thank you for viewing.

The error occurs due to your specify the parameter file name must be file in your controller method,but you do not set it in your client code

Two ways to solve it:

a. remove @RequestParam("file") to avoid specify the parameter name

b. add name property to your file element similar to below:

   <input type='file' name='file'>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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