我正在尝试在我的项目中实现烧瓶。但是,我遇到麻烦,因为我正在使用应用程序出厂功能,并且无法访问应用程序中其他文件的上传时间!
文件:app/__ init __。py
from flask_uploads import UploadSet, configure_uploads, IMAGES
def create_app(object_name):
...
images = UploadSet('images', IMAGES)
configure_uploads(app, images)
文件:App/Controllers/Editions.py
from flask_uploads import UploadNotAllowed
# this is one of my route files, registered in a blueprint
editions = Blueprint('editions', __name__)
...
# inside one of the routes
poster = request.files.get('poster')
try:
filename = images.save(poster)
except UploadNotAllowed:
flash('The upload was not allowed.', category='warning')
else:
# add to db...
当然这是不起作用的,因为images
未定义。但是如何从应用程序导入它?
我尝试了:
import app
app.images.save(...)
from flask import current_app
current_app.images.save(...)
from app import images
无效。有什么想法??
ps:我应用中的其他所有内容都可以正常工作。
我在这里发布后立即弄清楚了。它很简单,但找不到任何解释:
在您的控制器/视图文件上,路线所在:
from flask_upload import UploadSet, IMAGES
my_upload_set = UploadSet('my_upload_set', IMAGES)
# (...) routes where you can use:
image = request.files.get('image') # or whatever your form field name is
try:
filename = my_upload_set.save(image)
except UploadNotAllowed:
flash('The upload was not allowed.', category='warning')
else:
# add to db
在您的app/__init__.py
文件上:
from app.controllers.my_view import my_upload_set
from flask_uploads import configure_uploads
def create_app(object):
(...)
configure_uploads(app, my_upload_set)
这应该像魅力一样工作!任何问题让我知道...