0

Hi all I have a simple form with an upload button like this.

<form role="form" id="sf" name = "sf" method="post" 
  enctype="multipart/form-data" action="javascript:void();">
<input type="file" id="file" name="file" class="file">
</form>

my js looks like this.

$('#file').bind('change', function() {
        var form_data = new FormData($('#suject-form')[15]);

        var request = $.ajax({
         url: "/upload",
         type: "POST",
         contentType: "application/json",
         cache: false,
         processData: false,
         async: false,
         data: {
      file: new FormData($('#suject-form')[0]), 
      state: 'testing'
    },  
    dataType: "json",
  })
         .done(function(data){
          console.log(data);

        })
return false;
});

- When I try to get the file data it keeps giving me the 400 error,

-

@app.route('/upload', methods=['POST'])
def upload():

    if request.method == 'POST':
        state= request.form['state']
        files = request.files['file']
        # if I leave out file it returns but fails when leave in files


    return jsonify(result=[state])

Anyone know what I'm doing wrong? thanks!

Ahdee
  • 4,679
  • 4
  • 34
  • 58

0 Answers0