0%

Flask中的AJAX文件上传

表单:

<form method="post" enctype=multipart/form-data id='form1'>
    <input class="form-control" id="image" name="image" required type="file">
    <button type="submit" class="btn btn-default" id='submit1'>提交</button>
</form>

JS:

<script>
$('#image').change(function(){
    var form_data = new FormData($('#form1')[0]);

    $.ajax({
        url: '/signup/',
        type: 'post',
        data: form_data,
        contentType: false,
        processData: false,
        success: function(length){
            console.log('success');
        },
    });
})
</script>

Flask:

@app.route('/signup/', methods=['POST'])
def get_file():
    content = request.files.get('image1')