1

This is my web service's declaration

@POST
@Path("/upload")
@Consumes(MediaType.MULTIPART_FORM_DATA)
@Produces("application/json")
public DeviceDbUploadResponse upload(@FormDataParam("file1") InputStream file1, 
@FormDataParam("file2") InputStream file2,
@FormDataParam("name1") String filename1,
@FormDataParam("name2") String filename2,
@FormDataParam("ID") String ID)

My web service call

var fd=new FormData();
fd.append("ID",ID);
/* lines of code here */

$.ajax({
         url: 'http://localhost:8080/linterm2m/webapi/m2m/upload',
         data: fd,
         processData: false,
         contentType: false,
         type: 'POST'
       });

Everything works well so far. Now it is required to receive all the data (filename and ID) through an Request Object, something like:

public class Request{
    String ID;
    String filename1;
    String filename2;
}

But I doubt it can be fulfilled because of the multipart-form-data consuming type. I need some enlightenment and a solution.

dtxd
  • 49
  • 2
  • 11
  • 1
    You can get like this "@FormDataParam("file") InputStream uploadStream, @FormDataParam("file") FormDataContentDisposition fileDetail" with multipart-form-data if it is jersey based service – Nimesh Apr 10 '15 at 04:23
  • Yes, it seems neat now. But still, a Request object is required. – dtxd Apr 10 '15 at 06:06
  • Where are you passing request object? sorry I didn't look at your answer. You are done. Great! You can upvote my comments – Nimesh Apr 10 '15 at 07:14

1 Answers1

0

Composing multipart/form-data with a different Content-Type on each parts with Javascript (or Angular)

I try following the answer in this question and it works.

fd.append('whole', new Blob([JSON.stringify({
        ID: ID,
        name1:file1.name,
        name2:file2.name,
    })], {
        type: "application/json"
    }));

The Request class is just as I mentioned. The name1,name2 part is not needed but I just want to test with an Object with various attributes. Much appreciation for Naman's help about FormDataContentDisposition.

Community
  • 1
  • 1
dtxd
  • 49
  • 2
  • 11