title: File upload using ajax and rest web service
body: <p>I am using ajax and rest web service to upload files to the server. I have captured the file upload event and got the file object in my jquery method. I have to send the file object to web service so that i can save the file to db.
  Here is my jquery code..</p>
  
  <pre><code>  $scope.selectFile = function (fileObj) {
       if(fileObj!=undefined){
          $.ajax({
              url : "/RestServices/services/uploadFile",
              type : "POST",
              data : fileObj,
              processData: false,
              contentType: false,
              success : function(result) {
                  $scope.$apply();
              },
              error : function(xhr, tStatus, err) {
                  // alert(err);
              }
          });
       }
  </code></pre>
  
  <p>I have tried using FormData also, but I couldn't get the file in web service.</p>
  
  <pre><code>    var formData = new FormData(); 
      formData.append("fileToUpload", fileObj);   
  
       if(fileObj!=undefined){
          $.ajax({
              url : "/RestServices/services/uploadFile",
              type : "POST",
              data : formData,
              processData: false,
              contentType: false,
              success : function(result) {
                  $scope.$apply();
              },
              error : function(xhr, tStatus, err) {
                  // alert(err);
              }
  
          });
  </code></pre>
  
  <p>Here is my java code in service</p>
  
  <pre><code>@POST
  @Path("/uploadFile")
  @Consumes(MediaType.MULTIPART_FORM_DATA)
  public void uploadFile(File formData) {     
  try {
          System.out.println(formData.getFileName());             
     } catch (Exception e) {
      LOGGER.error("Exception in Upload File Endpoint"+ e.getMessage());
  }   
  }
  </code></pre>
  
  <p>If I send file Object directly without using formData, i am getting "media type not supported error" and if i send formData to service, i am getting temporary file only.
  What datatype should be there in ajax and in service methods? How can I get the uploaded file in the service? Any help will be appreciated.</p>