要上传文件,form必须要加enctype=multipart/form-data属性。
from flask_wtf.file import FileField
class MyForm(FlaskForm):
image = FileField('Upload Image:')
from werkzeug.utils import secure_filename
import os
my_form = MyForm()
if request.method == 'POST':
if my_form.validate_on_submit():
f = my_form.image.data
filename = secure_filename(f.filename)
f.save(os.path.join(current_app.instance_path, 'uploaded_files', filename))
uploaded_files目录需要存在,否则会报错。