Upload总是无限制地抛出

Upload总是无限制地抛出

本文介绍了Flask-Upload总是无限制地抛出"UploadNotAllowed"错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

烧瓶上传有一个叫做 UploadSet 的东西,它被描述为文件的单个集合".我可以使用此上传集将文件保存到预定义的位置.我已经定义了设置:

Flask-uploads has something called UploadSet which is described as a "single collection of files". I can use this upload set to save my file to a predefined location. I've defined my setup:

app = Flask(__name__)
app.config['UPLOADS_DEFAULT_DEST'] = os.path.realpath('.') + '/uploads'
app.config['UPLOADED_PHOTOS_ALLOW'] = set(['png', 'jpg', 'jpeg'])
app.config['MAX_CONTENT_LENGTH'] = 16 * 1024 * 1024

# setup flask-uploads
photos = UploadSet('photos')
configure_uploads(app, photos)

@app.route('/doit', method=["POST"])
def doit():
    myfile = request.files['file']
    photos.save(myfile, 'subfolder_test', 'filename_test')

    return ''' blah '''

这应该保存到./uploads/photos/subfolder_test/filename_test.png

我的测试图像是:2.6MB,是一个png文件.当我上传此文件时,出现错误:

My test image is: 2.6MB and is a png file. When I upload this file, I get the error:

...
File "/home/btw/flask/app.py", line 57, in doit
    photos.save(myfile, 'subfolder_test', 'filename_test')
File "/usr/local/lib/python2.7/dist-packages/flaskext/uploads.py", line 388, in save
    raise UploadNotAllowed()
UploadNotAllowed

但是,它并没有确切说明不允许的内容.我也尝试过删除所有约束,但是应用程序仍然会引发此错误.为什么?

However it doesn't say exactly what is not allowed. I have also tried removing all constraints, but the app still throws this error. Why?

编辑:

好的,所以我发现导致问题的实际上不是约束.导致该问题的是子文件夹和/或文件名:

Okay, so I figured out that it's not actually the constraints that is causing the problem. It is the subfolder and/or the filename that is causing the problem:

# This works
# saves to: ./uploads/photos/filename_test.png
photos.save(myfile)

但是我想保存到我的自定义位置 ./uploads/photos/< custom_subdir>/< custom_filename> .正确的方法是什么?

But I want to save to my custom location ./uploads/photos/<custom_subdir>/<custom_filename>. What is the correct way of doing this?

推荐答案

您还需要给 filename_test 扩展名

photos.save(myfile, 'subfolder_test', 'filename_test.png')

UploadSet 检查新文件名的扩展名,如果不允许新扩展名,则将引发异常.

The UploadSet checks the extension on the new file name and will throw the exception if the new extension is not allowed.

由于您没有给新文件扩展名,因此无法识别它.

Since you are not giving the new file an extension, it does not recognize it.

这篇关于Flask-Upload总是无限制地抛出"UploadNotAllowed"错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-18 10:10