How to upload a file using an ajax call in flask

后端 未结 3 1296
无人及你
无人及你 2020-11-30 00:14

Hi I\'m quite new to flask and I want to upload a file using an ajax call to the server. As mentioned in the documentation, I added a file upload to the html as folows:

3条回答
  •  伪装坚强ぢ
    2020-11-30 00:40

        in the  Javascript side::::        
    
        var form_data = new FormData();
        form_data.append('file', $('#uploadfile').prop('files')[0]);
    
        $(function() {
        $.ajax({
            type: 'POST',
            url:  '/uploadLabel',
            data: form_data,
            contentType: false,
            cache: false,
            processData: false,
            success: function(data) {
                console.log('Success!');
            },
        });
    
    
    
    in the server side::::
    
    
    @app.route('/uploadLabel',methods=[ "GET",'POST'])
    def uploadLabel():
        isthisFile=request.files.get('file')
        print(isthisFile)
        print(isthisFile.filename)
        isthisFile.save("./"+isthisFile.filename)
    

提交回复
热议问题