0

I am trying to send a multipart request to a controller service which looks like following

  @RequestMapping(value="/uploadFile", method=RequestMethod.POST)
    public void uploadApk(@RequestPart("fileName") String fileName, 
                    @RequestPart("md5") String md5, 
                    @RequestPart("userList") List<String> userList,
                    @RequestPart("file") MultipartFile file){
   ...
   ...
    }

The ajax request for calling the above function is

  var formData = new FormData();
  formData.append("fileName",imgfileList[0].name);
  formData.append("md5",md5);
  formData.append("userList",userList);
  formData.append("file", imgfileList[0]); 
 
  $.ajax({
    url: urlPost,
    type: "POST",
    data: formData,
    dataType: "json",
    processData: false,
    enctype:'multipart/form-data',
    headers: {'Content-Type': undefined},
    success: function(data)
    {
        alert("File Uploaded!");
    }
 });

I have tried to follow this link

But i am getting the following error.

{"timestamp":1434485164651,"status":415,"error":"Unsupported Media Type","exception":"org.springframework.web.HttpMediaTypeNotSupportedException","message":"Content type 'application/octet-stream' not supported","path":"/uploadFile"}

I tried to debug the error and found that the error was coming only for "@RequestPart("userList") List userList". That is the error appears only while sending an array of strings.

How do I solve the problem ?

Community
  • 1
  • 1
Soumya
  • 1,833
  • 5
  • 34
  • 45
  • Try removing `dataType: "json"` and `headers: {'Content-Type': undefined}` from your AJAX request – Arpit Aggarwal Jun 16 '15 at 09:59
  • I am going to try it. One more thing, when I try to do same thing using curl by typing "curl -X POST -F userList=[user1,user2] http://xxx.xxx.xxx.xxx:yyyy/apkStatus/uploadFile", I am getting the same error. No headers or content type has been provided here. Also, there is an implicit conversion of data to and from json by Jackson which done by Spring Framework itself whenever a request or response is processed – Soumya Jun 16 '15 at 10:06
  • I suspect you are going about this in an entirely wrong way. I don't think file uploads can be done by using $.ajax - what do you stick in the file property? The name of the file? For actual file upload, I think you have to utilize a form tag. – Fritz Duchardt Jun 16 '15 at 10:07
  • @CodeRunner: Actually I have checked the same with only the String List as MultipartRequest and getting the same error – Soumya Jun 16 '15 at 10:11
  • My point exactly - the whole approach trying to do a file upload with jquery is misguided to my knowledge. Have you tried something along the lines of http://www.journaldev.com/2573/spring-mvc-file-upload-example-tutorial-single-and-multiple-files – Fritz Duchardt Jun 17 '15 at 08:04

0 Answers0