2

I am sending a file via Ajax like this:

// Get the selected files from the input.
  var files = fileSelect.files;

// Create a new FormData object.
  var formData = new FormData();

    // Add the file to the request.
    formData.append('photos[]', files[0], files[0].name);


  $.ajax({
      type:"POST",
      url:"URL,
      dataType:"json",
      headers : {"cache-control":"no-cache"},
      timeout : 12000,
      processData: false,
      data:{
          formdata:formData
     }

Now I want to work with the send file in my java class, in a ressource like this:

@PermitAll
@POST
@Path(URL)
@Produces(MediaType.APPLICATION_JSON)
public Map<String, Object> fileHandler(@FormParam("formdata") File formdata){   }

But accessing the file does not work, @FormParam("formdata") File formdata seems to be wrong (or more things?). I want to get access to this file in my ressource class somehow. What am I doing wrong? Maybe someone knows a better solution for this.

user3813280
  • 137
  • 3
  • 11

2 Answers2

2

You can handle it this way:

I have changed the way FormData is passed. Used id of the form and passed it to create form data:

Javacript:

$.ajax({
            type : 'POST',
            url : rquestURL,
            cache:false,
            processData:false,
            contentType:false,
            data : new FormData($("#"+<your_form_id>)[0])}

Rresource (Added @Consumes(MediaType.MULTIPART_FORM_DATA) annotation):

@Path("/upload")
@POST
@Consumes(MediaType.MULTIPART_FORM_DATA)
public ResponseDTO doUpload(FormDataMultiPart multiPartData) {

    // non-file fields
    final String imageId = multiPartData.getField("<your_field_name>").getValue();

    // for file field    
    final FormDataBodyPart filePart = multiPartData.getField("<file_field_name>");
    final ContentDisposition fileDetails = filePart.getContentDisposition();
    final InputStream fileInputStream = filePart.getValueAs(InputStream.class);

    // use the above fields as required
    // file name can be accessed from field "fileDetails"  
    }
Pramod Karandikar
  • 5,289
  • 7
  • 43
  • 68
  • Thanks for answer. I tried that, but I get the error 'Cannot consume content type'. It seems that I send another format than the @consume expected. – user3813280 Feb 01 '15 at 12:27
  • 1
    Check the request type in the browser once you fire the request. It should be of type multi part form data. Check out the sample request in my answer. – Pramod Karandikar Feb 01 '15 at 13:52
  • Thanks, the error is gone. I am almost there, but I can´t fix another error: 'Could not find message body reader for type: class com.sun.jersey.core.header.FormDataContentDisposition of content type: multipart/form' – user3813280 Feb 02 '15 at 13:26
  • 1
    Check if you have all the required dependencies added in your classpath. You can once check this SO - http://stackoverflow.com/questions/15585074/jersey-client-exception-a-message-body-writer-was-not-found – Pramod Karandikar Feb 02 '15 at 13:34
  • I checked it, still no success. When I put '@FormDataParam("formdata") InputStream formdata' as parameter (only this), it works. When I put your parameters, the error occures. I tried to save the Input Stream to a file, but the outcoming file is always corrupted. Seems the input stream isnt correct. – user3813280 Feb 02 '15 at 14:58
1

When you deal with files, it's not just FormParam, it's FormDataParam.
Also, class File is for entity in your filesystem, not for files inside request. It should be InputStream instead.
So signature should look like this:

@PermitAll
@POST
@Path(URL)
@Produces(MediaType.APPLICATION_JSON)
public Map<String, Object> fileHandler(
    @FormDataParam("formdata") InputStream formdata,
    @FormDataParam("formdata") FormDataContentDisposition fileDetail
){   }

Here you could also see another parameter "FormDataContentDisposition", from it you could take details about data, like filename and size (would be useful, when you will read InputStream).
Note that I wrote this example for JAX-RS. Not sure what library you use.

mkrakhin
  • 3,386
  • 1
  • 21
  • 34