title: File is not uploading using Ajax/JQuery but uploading using Form submit
body: <p>I am trying to upload a file using using Ajax/Jquery, Actual image file is not getting uploaded, instead 0 byte file named "object_FileList" getting created on the destination directory.</p>
  
  <p>This is my JQuery Code.</p>
  
  <pre><code>$('#fileUpload').click(function() 
  {
      alert ('reached here');
      var photo_data = $("#photo")[0].files; // Getting the properties of file from file field
      var form_data = new FormData(); // Creating object of FormData class
      form_data.append('photo', photo_data);
  
      $.ajax({
          url: "upload.pl",
          cache: false,
          contentType: false,
          processData: false,
          data: form_data, // Setting the data attribute of ajax with file_data
          type: 'post',
          success : function(response)
          {
              alert ("success");
          },
          error: function(XMLHttpRequest, textStatus, errorThrown) 
          { 
  
            alert ("script error");
  
  
          }, // error 
      });
  });
  </code></pre>
  
  <p>HTML Code</p>
  
  <pre><code>&lt;input id="photo" type="file" name="photo" /&gt;
  &lt;button type='button' id='fileUpload' class='btn btn-primary btn-sm'&gt;
     &lt;span class='glyphicon glyphicon-upload'&gt;&lt;/span&gt; 
     Start Upload
  &lt;/button&gt;
  </code></pre>
  
  <p>Perl Script</p>
  
  <pre><code>#!c:/perl64/bin/perl.exe
  
  use strict;
  use CGI;
  use CGI::Carp qw ( fatalsToBrowser );
  use File::Basename;
  
  $CGI::POST_MAX = 1024 * 5000;
  my $safe_filename_characters = "a-zA-Z0-9_.-";
  my $upload_dir = "C:/Users/Public/";
  
  my $cgi = new CGI;
  my $filename = $cgi-&gt;param("photo");
  
  if ( !$filename )
  {
      print $cgi-&gt;header ( );
      print "There was a problem uploading your photo (try a smaller file).";
      exit;
  }
  
  
  my ( $name, $path, $extension ) = fileparse ( $filename, '..*' );
  $filename = $name . $extension;
  $filename =~ tr/ /_/;
  $filename =~ s/[^$safe_filename_characters]//g;
  
  if ( $filename =~ /^([$safe_filename_characters]+)$/ )
  {
      $filename = $1;
  }
  else
  {
      die "Filename contains invalid characters";
  }
  
  
  my $upload_filehandle = $cgi-&gt;upload("photo");
  print $upload_filehandle;
  
  
  open UPLOADFILE, "&gt;C:/Users/Public/$filename"  or die $!;
  binmode UPLOADFILE;
  
  while ( &lt;$upload_filehandle&gt; )
  {
  print UPLOADFILE;
  }
  
  close UPLOADFILE;
  
  print $cgi-&gt;header ();
  </code></pre>
  
  <p>I don't think there is any issue in Perl script because if I'm using form data to upload the file, its uploading perfectly.</p>
  
  <pre><code>&lt;form action="upload.pl" method="post" enctype="multipart/form-data"&gt;
          &lt;input id="photo" type="file" name="photo" /&gt;
          &lt;input type="submit" name="Submit" value="Submit Form" /&gt;
  &lt;/form&gt;
  </code></pre>
  
  <p>Can someone help me what is wrong in first case. (Uploading using Ajax/Jquery)??</p>