Ido_f
Ido_f

Reputation: 719

Jersey upload receives request meta instead of file

I've had this code working and well functioning, yet now, couple of revisions later, when I go back to its upload function, it seems to only read the meta of the request and not the actual file. So, for example, that's what I get when trying to upload a txt file:

------WebKitFormBoundaryF5IARVFzqsY3zpj4Content-Disposition: form-data; name="file"undefined------WebKitFormBoundaryF5IARVFzqsY3zpj4--

The JS:

$scope.setFiles = function(element) {
    $scope.$apply(function($scope) {
        console.log('files:', element.files[0]);
        // Turn the FileList object into an Array
        $scope.files = element.files[0];
        $scope.progressVisible = false;
    });
};

$scope.analyseFile = function() {
    var fd = new FormData();
    fd.append("file", $scope.files[0]);

    var xhr = new XMLHttpRequest();
    xhr.addEventListener("load", uploadComplete, false);
    xhr.addEventListener("error", uploadFailed, false);
    xhr.addEventListener("abort", uploadCanceled, false);
    xhr.open("POST", "/rest/analysisController/analyseFile");
    $scope.progressVisible = false;
    xhr.send(fd);
};

and the server side:

@POST
@Path("analyseFile")
@Produces("application/json; charset=UTF-8")
@Consumes(MediaType.MULTIPART_FORM_DATA)
public Response analyseFile(
        @FormDataParam("file") InputStream uploadedInputStream) throws
        RemoteException {

    System.out.println(getStringFromInputStream(
            uploadedInputStream));

    return Response.ok().build();
}

I am also including the multipart dependency:

<dependency>
        <groupId>org.glassfish.jersey.media</groupId>
        <artifactId>jersey-media-multipart</artifactId>
        <version>2.5.1</version>
        <scope>provided</scope>
    </dependency>

Upvotes: 0

Views: 1037

Answers (1)

Ido_f
Ido_f

Reputation: 719

So it turns out my problem was in switching to the minified version of 'angular-file-upload.js'. The minified version didn't work for some reason and therefore it didn't actually send the content of the file.

I'm not sure whether this answer will actually help anyone, but just in case.

Upvotes: 1

Related Questions