您好,登錄后才能下訂單哦!
參考: http://www.cnblogs.com/himir/p/5940705.html https://zhuanlan.zhihu.com/p/24423891 flask-uploads flask的一個文件上傳擴展, 提供了UploadSet這個概念 flask-wtf(中文) 很強大的表單的擴展 flask-bootstrap bootstrap的flask擴展, 結合模版使用, 此處用到quick_form功能 from flask import Flask, render_templatefrom flask_uploads import UploadSet, IMAGES, configure_uploadsfrom flask_wtf import Formfrom wtforms import SubmitFieldfrom flask_wtf.file import FileField, FileAllowed, FileRequiredfrom flask_bootstrap import Bootstrap from werkzeug.utils import secure_filenameapp = Flask(__name__)# 新建一個set用于設置文件類型、過濾等set_mypic = UploadSet('mypic') # mypic# 用于wtf.quick_form()模版渲染bootstrap = Bootstrap(app)# mypic 的存儲位置, # UPLOADED_xxxxx_DEST, xxxxx部分就是定義的set的名稱, mypi, 下同app.config['UPLOADED_MYPIC_DEST'] = './static/img'# mypic 允許存儲的類型, IMAGES為預設的 tuple('jpg jpe jpeg png gif svg bmp'.split()) #DOCUMENTS = tuple('rtf odf ods gnumeric abw doc docx xls xlsx'.split()) #AUDIO = tuple('wav mp3 aac ogg oga flac'.split()) #TEXT = ('txt',) #DATA = ... #SCRIPTS = ... #ARCHIVES = ... #EXECUTABLES = ... #DEFAULT = TEXT + DOCUMENTS + IMAGES + DATAapp.config['UPLOADED_MYPIC_ALLOW'] = IMAGES# 把剛剛app設置的config注冊到set_mypicconfigure_uploads(app, set_mypic) # 此處WTF的SCRF密碼默認為和flask的SECRET_KEY一樣 # app.config['WTF_CSRF_SECRET_KEY'] = 'wtf csrf secret key'app.config['SECRET_KEY'] = 'xxxxx' #允許上傳的文件最大為10Mpatch_request_class(app,10*1024*1024) class UploadForm(Form): # 文件field設置為‘必須的’,過濾規則設置為‘set_mypic’ upload = FileField('p_w_picpath', validators=[FileRequired('文件未選擇!'), FileAllowed(set_mypic, '只能上傳圖片!')]) submit = SubmitField('上傳') @app.route('/', methods=('GET', 'POST'))def index(): form = UploadForm() url = None if form.validate_on_submit(): filename = secure_filename(form.upload.data.filename) fileupload = set_mypic.save(form.upload.data, name=filename) url = set_mypic.url(fileupload) return render_template('index.html', form=form, url=url)if __name__ == '__main__': app.run(debug=True) ----------------------------------------- 安全上傳方法: from flask_wtf import FlaskFormfrom flask_wtf.file import FileField, FileRequiredfrom werkzeug.utils import secure_filename form = FlaskForm(csrf_enabled=False) WTF_CSRF_ENABLED = False WTF_CSRF_SECRET_KEY = 'a random string' class PhotoForm(FlaskForm): photo = FileField(validators=[FileRequired()]) @app.route('/upload', methods=['GET', 'POST'])def upload(): if form.validate_on_submit(): f = form.photo.data filename = secure_filename(f.filename) f.save(os.path.join(app.instance_path, 'photos', filename)) return redirect(url_for('index')) return render_template('upload.html', form=form) ------------------------------------------------ html文件: {% extends "base.html" %} {% import "bootstrap/wtf.html" as wtf %} {% block page_content %} {{ wtf.quick_form(form, enctype="multipart/form-data") }} <h5>文件上傳成功!</h5> {% if url %} <br><img src="{{ url }}"> {% endif %} {% endblock page_content %} 注: 上傳文件的 form 必須是 POST 方法,并且 enctype=multipart/form-data。type=file。 如果使用 GET 方法,則只會上傳文件名。 如何將照片資源與用戶進行綁定? 使用輕量級 ORM 框架,peewee,使用兩個字段 use_id、photo_name。將數據存儲到數據庫表格中。需要時可根據登錄 user 的 id 獲取到對應的 photo_name,然后使用 photos.url(photo_name) 進行加載。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。