1

My angular controller is

app1.controller('kyc_controller', ['$scope', '$http', function($scope, $http)
{    
    $scope.uploadFile = function(){
         var file = $scope.myFile;
         var fd = new FormData();
         fd.append('file', file);
         $http({
            method : 'POST',
            url: '/GngService/api/GoNoGo/upload_file',
            params : {'filename':'pancard'},
            data : fd,
            transformRequest: angular.identity,
            headers: {'Content-Type': undefined}
         }).success(function (data, status, headers, config) {
            console.log('file ' + config.file.name + 'uploaded. Response: ' + JSON.stringify(data));
         });
    };

}]);

and my server side api method is

@POST
    @Path("/upload_file")
    @Consumes({MediaType.MULTIPART_FORM_DATA,MediaType.APPLICATION_FORM_URLENCODED})
    @Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
        public Response upload(@FormDataParam("file")  InputStream ins, @FormDataParam("filename")  String filename,  @FormDataParam("file") FormDataContentDisposition contentDispositionHeader ) throws IOException {
            System.out.println(filename);
            BufferedImage image = ImageIO.read(ins);
            // ImageIO.write(image, "jpg", new File("/../../fileName"));
            String result = "{'status' : 'ok'}";
            String json = new Gson().toJson(result);
            return Response.ok(json.toString(), MediaType.APPLICATION_JSON).build();
        }

It shows Unsupported media type...please help me to upload images....thanks in advance

kshreve
  • 310
  • 3
  • 14
Sumit Kumar
  • 394
  • 2
  • 20
  • 1
    I think you should not set Content-Type to undefined at the http request. It exists some image/[someFormat] content types that may help you to upload your files. – Tony Apr 02 '15 at 12:57
  • actually i want to upload any type of file...but its not working – Sumit Kumar Apr 02 '15 at 13:02
  • Your issue should be related to this post : http://stackoverflow.com/questions/21879524/angularjs-upload-and-post-photo-as-multi-part-form-data-and-get-response – Tony Apr 02 '15 at 13:06
  • i am also using directives....and also try with $upload .....i think this problem is with java code – Sumit Kumar Apr 02 '15 at 13:19
  • The problem is both java and javascript code : the @Consumes ... mediaType must match with what you send over the Content-Type header from the javascript code. "In the Content-Type header of an HTTP/1.1 request or response, a media type reference indicates the "type" associated with the message body's byte sequence" (from : http://www.programmableweb.com/news/rest-api-design-put-type-content-type/2011/11/18) – Tony Apr 02 '15 at 15:40
  • on server its shows following error..............even i have included all the jersey libs SEVERE: Missing dependency for method public javax.ws.rs.core.Response org.gng.webservice.GngService.upload(java.io.InputStream,com.sun.jersey.core.header.FormDataContentDisposition) at parameter at index 0 – Sumit Kumar Apr 03 '15 at 06:15
  • 1
    This issue may be related to this other post : http://stackoverflow.com/questions/18431012/cannot-implement-simple-file-upload-in-jersey-annotated-with-post-of-resource. it's seems you may have to change jersey versions and change the names of the parameter you passed into : @FormDataParam("file") – Tony Apr 03 '15 at 08:09

0 Answers0