title: Python File Upload: Ajax with Multiple Other Fields, and Flask Upload
body: <p>I am getting an internal server error 500 when I am uploading files with multiple other fields that I would like to use in the same method. However I can't seem to figure out what exactly is wrong. I am using Flask Upload in Python for this and I am not sure if I am missing something that is obvious. Below is the HTML, Javascript, and Python that I am using. Any help would be greatly appreciated.</p>
  
  <pre><code>&lt;form method=POST enctype=multipart/form-data id="contentAddForm" class="pure-form-stacked"&gt;
          &lt;input id='content_name' placeholder='name'/&gt;
          &lt;select id='selectOptions'&gt;
          &lt;/select&gt;
          &lt;input type="file" id="file" name="file"&gt;
          &lt;input type="submit" value="Submit"&gt;
  &lt;/form&gt;
  </code></pre>
  
  <p>This is my ajax, I got this adapted from answers here: <a href="https://stackoverflow.com/questions/6974684/how-to-send-formdata-objects-with-ajax-requests-in-jquery">How to send FormData objects with Ajax-requests in jQuery?</a></p>
  
  <pre><code>$("#contentAddForm").submit(function(e) {
         e.preventDefault(); 
  
  
        if($("#name").val() == ""){
          alert("No name picked");
          return;
        }
  
  
         var fd = new FormData();    
         fd.append('file', $( '#file' )[0].files[0]);
         fd.append('name', $("#content_name").val());
         fd.append('selectedOption', $('#selectOptions option:selected').val());
  
          $.ajax({
              type: 'POST',
              url: '/dbUserContentAdd',
              data: fd,
              contentType: false,
              cache: false,
              processData: false,
              async: false,
              success: function(data) {
                  alert(data);
              },
              failure: function(data){
                  alert(data);
              }
          });
  
  
  
      });
  </code></pre>
  
  <p>This is my python side, adapted from <a href="http://flask.pocoo.org/docs/0.10/patterns/fileuploads/" rel="nofollow noreferrer">http://flask.pocoo.org/docs/0.10/patterns/fileuploads/</a> </p>
  
  <pre><code> #access via myURL.com/static/img/myImage.png
   UPLOAD_FOLDER = '/static/img'
   ALLOWED_EXTENSIONS = set(['png', 'jpg', 'jpeg', 'gif'])
  
   @app.route('/dbUserContentAdd', methods=['GET', 'POST'])
   @login_required
   @confirm_email_required
   def upload_file():
       if request.method == 'POST':
           file = request.files['file']
           if file and allowed_file(file.filename):
               filename = secure_filename(file.filename)
               file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
               name = request.form.get('name', '')
               selectedOption = request.form.get('selectedOption', '')
                  ...
               return "SUCCESS"
  
       return 'NOT_POST'
  </code></pre>