-
-
Save greyli/81d7e5ae6c9baf7f6cdfbf64e8a7c037 to your computer and use it in GitHub Desktop.
| <!-- create a folder named templates, put this file into it --> | |
| <!DOCTYPE html> | |
| <title>Upload File</title> | |
| <h1>Photo Upload</h1> | |
| <form method="POST" enctype="multipart/form-data"> | |
| {{ form.hidden_tag() }} | |
| {{ form.photo }} | |
| {% for error in form.photo.errors %} | |
| <span style="color: red;">{{ error }}</span> | |
| {% endfor %} | |
| {{ form.submit }} | |
| </form> | |
| {% if file_url %} | |
| <br> | |
| <img src="{{ file_url }}"> | |
| {% endif %} |
| # -*- coding: utf-8 -*- | |
| import os | |
| from flask import Flask, render_template | |
| from flask_uploads import UploadSet, configure_uploads, IMAGES, patch_request_class | |
| from flask_wtf import FlaskForm | |
| from flask_wtf.file import FileField, FileRequired, FileAllowed | |
| from wtforms import SubmitField | |
| basedir = os.path.abspath(os.path.dirname(__file__)) | |
| app = Flask(__name__) | |
| app.config['SECRET_KEY'] = 'I have a dream' | |
| app.config['UPLOADED_PHOTOS_DEST'] = os.path.join(basedir, 'uploads') # you'll need to create a folder named uploads | |
| photos = UploadSet('photos', IMAGES) | |
| configure_uploads(app, photos) | |
| patch_request_class(app) # set maximum file size, default is 16MB | |
| class UploadForm(FlaskForm): | |
| photo = FileField(validators=[FileAllowed(photos, 'Image only!'), FileRequired('File was empty!')]) | |
| submit = SubmitField('Upload') | |
| @app.route('/', methods=['GET', 'POST']) | |
| def upload_file(): | |
| form = UploadForm() | |
| if form.validate_on_submit(): | |
| filename = photos.save(form.photo.data) | |
| file_url = photos.url(filename) | |
| else: | |
| file_url = None | |
| return render_template('index.html', form=form, file_url=file_url) | |
| if __name__ == '__main__': | |
| app.run() |
app.config['UPLOADED_PHOTOS_DEST'] = os.getcwd() + '/app/tmp/'The above change will send the photo to a directory other than the current directory.
Thank you very much for the code!
请问,用Flask_uploads做了一个上传手机照片的页面,但偶尔还是会有
raise UploadNotAllowed()
flask_uploads.UploadNotAllowed
想用try/except查找此情况下上传文件的扩展名。
但是:
except UploadNotAllowed as error:
会报错:
NameError: name 'UploadNotAllowed' is not defined
还试过flask_uploads.UploadNotAllowed或flask_uploads,但都说没定义。
请问这里该怎么写?感谢~
I using Flask_uploads to make a page to upload mobile photos,
but occasionally there still be
raise UploadNotAllowed()
flask_uploads.UploadNotAllowed
I want to use try / except to find the extension of the uploaded file.
but:
except UploadNotAllowed as error:
Will report an error:
NameError: name 'UploadNotAllowed' is not defined
I also tried flask_uploads.UploadNotAllowed or flask_uploads, but they all undefined.
What should I write here? thank~
请问,用Flask_uploads做了一个上传手机照片的页面,但偶尔还是会有
raise UploadNotAllowed() flask_uploads.UploadNotAllowed想用try/except查找此情况下上传文件的扩展名。
但是:
except UploadNotAllowed as error:
会报错:
NameError: name 'UploadNotAllowed' is not defined
还试过flask_uploads.UploadNotAllowed或flask_uploads,但都说没定义。
请问这里该怎么写?感谢~I using Flask_uploads to make a page to upload mobile photos,
but occasionally there still beraise UploadNotAllowed() flask_uploads.UploadNotAllowedI want to use
try / exceptto find the extension of the uploaded file.
but:
except UploadNotAllowed as error:
Will report an error:
NameError: name 'UploadNotAllowed' is not defined
I also triedflask_uploads.UploadNotAllowedorflask_uploads, but they all undefined.
What should I write here? thank~
ok,i konw,need from flask_uploads import UploadNotAllowed
Does IMAGES support video format too?
@StarORC
Thank you so much!