美文网首页
Flask 上传文件到服务器

Flask 上传文件到服务器

作者: 是光就会灿烂 | 来源:发表于2019-03-22 11:07 被阅读0次

    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表单即可

    <!doctype html>

        <title>Upload new File</title>

        <h1>上传文件</h1>

        <form action="" method=post enctype=multipart/form-data>

          <p><input type=file name=file>

            <input type=submit value=上传>

        </form>

    相关文章

      网友评论

          本文标题:Flask 上传文件到服务器

          本文链接:https://www.haomeiwen.com/subject/atjcvqtx.html