处理文件上传,使用枕头调整大小,使用SQLAlchemy存储,使用烧瓶提供文件



所以我正在尝试处理文件上传,然后将该文件作为二进制文件存储到数据库中。存储后,我尝试在给定的URL上提供文件。我似乎找不到在这里有效的方法。我需要使用数据库,因为我正在使用Google应用引擎进行托管。以下是我的代码的一些相关部分。

服务处理程序:

@app.route('/photo/<path:filename>')
def uploaded_photo(filename):
try:
photo = db_session.query(Photo)
.filter_by(filename=filename).one()
except:
return 'There was an error with the photo.'
return send_file(
io.BytesIO(photo.image_blob),
attachment_filename=photo.filename,
mimetype='image/jpeg')

我如何使用枕头存储我的图像。

Photo(
filename=filename,
image_blob=image.tobytes()))

照片模型:

class Photo(Base):
__tablename__ = 'photos'
id = Column(Integer, primary_key=True)
item_id = Column(Integer, ForeignKey('item.id'))
filename = Column(String)
image_blob = Column(LargeBinary)
item = relationship("Item", back_populates="photos")

发生的情况是,当您加载图像网址/photo/时。出现一张空白照片。但是,上传部分进展顺利。

不要将图像存储在数据库中,而是使用可以保存文件的静态文件夹。 只需将文档名称和详细信息保存在数据库中即可进行文件检索。 您可以查看此文档。上传链接....这是用于下载...链接

相关内容

最新更新