[Flask]文件上传Demo

参考flask官方文档:http://flask.pocoo.org/docs/0.12/patterns/fileuploads/

  • boot.py
#!/usr/bin/env python
# -*- encoding: utf-8 -*-
# Author : changyanlong

from flask import Flask,render_template,request,redirect,url_for
from werkzeug.utils import secure_filename
import os

app = Flask(__name__)

@app.route('/upload', methods=['POST', 'GET'])
def upload():
    if request.method == 'POST':
        f = request.files['file']
        # basepath = os.path.dirname(__file__)  # 当前文件所在路径
        basepath = sys.path[0]  # 项目的根目录 boot_main.py目录
        upload_path = os.path.join(basepath, 'static\uploads',secure_filename(f.filename))  #注意:没有的文件夹一定要先创建,不然会提示没有该路径
        f.save(upload_path)
        return redirect(url_for('upload'))
    return render_template('upload.html')

if __name__ == '__main__':
    app.run(debug=True)
  • upload.html



    
    Title


    

文件上传示例

你可能感兴趣的:([Flask]文件上传Demo)