13

I'm implementing a file upload in a web application.

The front-end is written in angularJS and uses the angular-file-upload package (the one that can be found at this link https://github.com/nervgh/angular-file-upload).

The back-end is Java / Jersey Web Services.

My Question is:

The uploaded file contains a WebKitFormBoundary header and footer, like this:

------WebKitFormBoundarylqskdjlqksdjl
Content-Disposition: form-data; name="upload"; filename="foo.bar"
Content-Type: multipart/form-data

Therefore, I'm not sure whether I'm uploading a file or a request. And of course, my back-end application considers that the uploaded files are corrupted and would not display them unless those lines are removed (for now manually).

Bottom line is : how do I get rid of that header and footer in the uploaded file?

Here are some code samples.

Front-End

Once again: angularJS angular-file-upload

item.headers = {
    'Content-Disposition': 'attachment; filename="' + item.file.name + '"',
    'Content-Type': 'multipart/form-data'
};

Back-End

and Java / Jersey

@POST
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.MULTIPART_FORM_DATA)
@Path("someurl/{fileName}")
public Artifact uploadArtifact(InputStream uploadedStream, @PathParam("fileName") String fileName) throws Exception;

Note

I'm wondering if the Content-Disposition: attachment in my angularJS part could be what's messing it up?

And that it should rather be Content-Disposition: form-data?

Thx in advance!

vsminkov
  • 10,912
  • 2
  • 38
  • 50
avi.elkharrat
  • 6,100
  • 6
  • 41
  • 47
  • We used both angular-file-upload and java backend in our project. What i know is that you don't need to manipulate any header in ajax request. You should handle it in java, maybe you need a library there to handle these issues. – Morteza Mar 21 '16 at 20:53

1 Answers1

3

You need to place @FormDataParam annotation in order to properly handle boundary.

@POST
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.MULTIPART_FORM_DATA)
@Path("someurl/{fileName}")
public Artifact uploadArtifact(
    @FormDataParam("file") InputStream uploadedStream,
    @FormDataParam("file") FormDataContentDisposition fileDetails,
    @PathParam("fileName") String fileName) throws Exception;
vsminkov
  • 10,912
  • 2
  • 38
  • 50