1

I am trying to upload a file and read it at the server side. But i am not able to read the file instead i am getting an exception

Required MultipartFile parameter 'file' is not present

Below is the code snippet for the same. Can you kindly tell me if i am doing something wrong here. Is there any other way to read the file sent by an ajax request at the server end.

<form  id="dealform" method="post" enctype="multipart/form-data" type="file">
<input type="file" name="file" id="upload_file" style="visibility: hidden;width:0px;height:0px;"/><input id="fg-upload-button" type="submit" value="Upload" style="display:none;"/>
</form>

this.getRecord              = function(params)      {

            var file = $('#upload_file').prop("files")[0];

            $.ajax({
                url         : /Upload,
                data        : file,
                type        : 'POST',
                dataType    : 'json',
                timeout     : json_timeout,
                error       : function(){
                    that.notifyGetDataError('error getting:');                  
                },
                success     : function(data){
                    that.notifyGetDataSuccess();
                }
            });
        };

In the controller :

@RequestMapping(value = "/Upload.json", method = RequestMethod.POST)
    public ModelAndView getContents(@RequestParam("file") MultipartFile file) { 
}

Using the below in applicationContext.xml

<bean id="multipartResolver"
   class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
</bean>
borisdiakur
  • 10,387
  • 7
  • 68
  • 100

4 Answers4

0

The code is passing the file itself as the payload to your server. However your controller is expecting the file to be send as a value of parameter "file"

Karthik
  • 1,005
  • 8
  • 7
0

You are sending your file as dataType="json" which may be causing you problem because your content-type is multipart/form-data

Follow this link FormData for ajax file upload

Another link for your problem is here

You can follow this link which has controller code for MultipartFile

Community
  • 1
  • 1
Shoaib Chikate
  • 8,665
  • 12
  • 47
  • 70
0
 data        : {file1:file}

and in controller

getContents(@RequestParam("file1") MultipartFile file)
Jens
  • 67,715
  • 15
  • 98
  • 113
anind
  • 1
0

In controller method, just change the name of the request parameter to data:

@RequestMapping(value = "/Upload.json", method = RequestMethod.POST)
    public ModelAndView getContents(@RequestParam("data") MultipartFile file) { 
}
sandeep23
  • 16
  • 4