上传具有不允许的文件类型或没有文件的文件时处理 Flask-Reuploaded 错误
Handling Flask-Reuploaded error when uploading a file with a disallowed filetype or no file
您好,
我正在尝试处理上传文件类型不允许的文件或在未选择任何文件的情况下提交的错误。
我得到的只是 "flask_uploads.exceptions.UploadNotAllowed" 和网页上的 "Internal Server Error"。我想显示一条错误消息,但不知道如何处理该错误。我用谷歌搜索并阅读了文档,但找不到方法。
选择并提交正确类型的 file/files(在本例中为 IMAGES)时一切正常。
谢谢!
if request.method == 'POST':
if form.validate_on_submit() and 'artwork' in request.files:
art = form.artwork.data
this_artf = art_f+'/'+str(order_no_art)
app.config['UPLOADED_IMAGES_DEST'] = this_artf
images = UploadSet('images', IMAGES)
configure_uploads(app, images)
for image in art:
images.save(image)
在 UploadSet
的实例上处理 flask_uploads.exceptions.UploadNotAllowed
exception when invoking save
method。
from flask_uploads.exceptions import UploadNotAllowed
from flask import flash # make sure to configure secret key for your app
#....
error_files = []
for image in art:
try:
images.save(image)
except flask_uploads.exceptions.UploadNotAllowed as err:
error_files.append(image.filename)
continue
flash(error_files, 'error')
然后,您可以通过获取 flashed messages.
在呈现的模板中显示文件
正如您所说,您已经查看了文档...
我刚刚通过处理提到的 UploadNotAllowed
异常增强了 https://github.com/jugmac00/flask-reuploaded 中显示的示例。
一定不要忘记先导入异常!
...
from flask_uploads.exceptions import UploadNotAllowed
...
@app.route("/", methods=['GET', 'POST'])
def upload():
if request.method == 'POST' and 'photo' in request.files:
try:
photos.save(request.files['photo'])
flash("Photo saved successfully.")
return render_template('upload.html')
except UploadNotAllowed:
flash("File type not allowed!")
return render_template('upload.html')
return render_template('upload.html')
这是一个通用的答案,但我相信您可以将其应用到您的案例中。
顺便说一下,我看到您在请求处理中配置了应用程序:
if request.method == 'POST':
if form.validate_on_submit() and 'artwork' in request.files:
art = form.artwork.data
this_artf = art_f+'/'+str(order_no_art)
app.config['UPLOADED_IMAGES_DEST'] = this_artf
images = UploadSet('images', IMAGES)
configure_uploads(app, images)
虽然这目前有效,但这不是它应该做的方式。
是关于这几行...
app.config['UPLOADED_IMAGES_DEST'] = this_artf
images = UploadSet('images', IMAGES)
configure_uploads(app, images)
您必须将这些行移到请求上下文之外,例如在模块顶部或应用程序工厂中。
免责声明:我是Flask-Reuploaded
的维护者。
您好,
我正在尝试处理上传文件类型不允许的文件或在未选择任何文件的情况下提交的错误。
我得到的只是 "flask_uploads.exceptions.UploadNotAllowed" 和网页上的 "Internal Server Error"。我想显示一条错误消息,但不知道如何处理该错误。我用谷歌搜索并阅读了文档,但找不到方法。
选择并提交正确类型的 file/files(在本例中为 IMAGES)时一切正常。
谢谢!
if request.method == 'POST':
if form.validate_on_submit() and 'artwork' in request.files:
art = form.artwork.data
this_artf = art_f+'/'+str(order_no_art)
app.config['UPLOADED_IMAGES_DEST'] = this_artf
images = UploadSet('images', IMAGES)
configure_uploads(app, images)
for image in art:
images.save(image)
在 UploadSet
的实例上处理 flask_uploads.exceptions.UploadNotAllowed
exception when invoking save
method。
from flask_uploads.exceptions import UploadNotAllowed
from flask import flash # make sure to configure secret key for your app
#....
error_files = []
for image in art:
try:
images.save(image)
except flask_uploads.exceptions.UploadNotAllowed as err:
error_files.append(image.filename)
continue
flash(error_files, 'error')
然后,您可以通过获取 flashed messages.
在呈现的模板中显示文件正如您所说,您已经查看了文档...
我刚刚通过处理提到的 UploadNotAllowed
异常增强了 https://github.com/jugmac00/flask-reuploaded 中显示的示例。
一定不要忘记先导入异常!
...
from flask_uploads.exceptions import UploadNotAllowed
...
@app.route("/", methods=['GET', 'POST'])
def upload():
if request.method == 'POST' and 'photo' in request.files:
try:
photos.save(request.files['photo'])
flash("Photo saved successfully.")
return render_template('upload.html')
except UploadNotAllowed:
flash("File type not allowed!")
return render_template('upload.html')
return render_template('upload.html')
这是一个通用的答案,但我相信您可以将其应用到您的案例中。
顺便说一下,我看到您在请求处理中配置了应用程序:
if request.method == 'POST':
if form.validate_on_submit() and 'artwork' in request.files:
art = form.artwork.data
this_artf = art_f+'/'+str(order_no_art)
app.config['UPLOADED_IMAGES_DEST'] = this_artf
images = UploadSet('images', IMAGES)
configure_uploads(app, images)
虽然这目前有效,但这不是它应该做的方式。
是关于这几行...
app.config['UPLOADED_IMAGES_DEST'] = this_artf
images = UploadSet('images', IMAGES)
configure_uploads(app, images)
您必须将这些行移到请求上下文之外,例如在模块顶部或应用程序工厂中。
免责声明:我是Flask-Reuploaded
的维护者。