Flask 上传文件到服务器

import os

from flaskimport Flask, request, redirect, url_for,render_template

from werkzeug.utilsimport secure_filename

UPLOAD_FOLDER= '/home/liji/works/LJfile/test1'

ALLOWED_EXTENSIONS= set(['txt', 'pdf', 'png', 'jpg', 'jpeg', 'gif'])

app= Flask(__name__)

app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER

@app.route('/succe')

def succes():

    return render_template('succe.html')

def allowed_file(filename):

    return '.' in filenameand \

filename.rsplit('.', 1)[1] in ALLOWED_EXTENSIONS

@app.route('/', methods=['GET', 'POST'])

def upload_file():

    if request.method== 'POST':

        file= request.files['file']

        if fileand allowed_file(file.filename):

            filename= secure_filename(file.filename)

            file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))

            return redirect('succe')

    return render_template('upload.html')

if __name__=='__main__':

    app.debug=True

    app.run()


前端编写一个from表单即可

    Upload new File

   

上传文件

   

     

       

   

你可能感兴趣的:(Flask 上传文件到服务器)