title: Upload file using Python AND Ajax Jquery
body: <p>Here Is my html with jquery/ajax call section </p>
  
  <pre><code>&lt;form id="upload-file" role="form" action="sendQuestions" method="post" enctype="multipart/form-data"&gt;
  &lt;div class="modal-body"&gt;
      &lt;label for="file"&gt;&lt;b&gt;Upload packet here&lt;/b&gt;&lt;/label&gt;
      &lt;input type="file" name="file"&gt;
      &lt;p class="help-block"&gt;Upload a .pdf or .docx file you want to read.&lt;/p&gt;
  &lt;/div&gt;
  &lt;div class="modal-footer"&gt;
      &lt;button type="button" class="btn btn-default" data-dismiss="modal"&gt;Close&lt;/button&gt;
      &lt;button id="upload-file-btn" type="button" class="btn btn-primary" data-dismiss="modal" value="Upload"&gt;Upload&lt;/button&gt;
  &lt;/div&gt;
  &lt;/form&gt;
  
  &lt;script&gt;
  
  $(function() {
  $('#upload-file-btn').click(function() {
      var form_data = new FormData($('#upload-file')[0]);
      $.ajax({
          type: 'POST',
          url: 'abc.py',
          data: form_data,
          contentType: false,
          cache: false,
          processData: false,
          async: false,
          success: function(data) {
              console.log('Success!');
          },
      });
  });
  });
  </code></pre>
  
  <p>Now here is my python abc.py section </p>
  
  <pre><code>#!C:/Python27/python.exe
  #print "Content-type: text/html\r\n\r\n";
  #//20256542
  
  import cgi, os
  import cgitb; cgitb.enable()
  import requests
  
  form = cgi.FieldStorage()
  
  
  fileitem = form.getvalue('file')
  
  # Test if the file was uploaded
  if fileitem.filename:
     # strip leading path from file name to avoid 
     # directory traversal attacks
     fn = os.path.basename(fileitem.filename.replace("\\", "/" ))
     open(fn, 'wb').write(fileitem.file.read())
  
     print 'PASS'
  
  else:
     print 'FAILED'
  </code></pre>
  
  <p>Now when my <code>ajax</code> calls the <code>abc.py</code> I got the error :
  Error at:</p>
  
  <pre><code> if fileitem.filename:
  
  &lt;type 'exceptions.AttributeError'&gt;: 'NoneType' object has no attribute 'filename'
    args = ("'NoneType' object has no attribute 'filename'",)
    message = "'NoneType' object has no attribute 'filename'" 
  </code></pre>
  
  <p>i understand that through request I think we cant access the file in abc.py , if i do the same by <code>form action</code> I can upload the file. But not incase of this <code>ajax call</code> .  Please help</p>