sách gpt4 ai đã đi

python - Xử lý việc tải tệp lên, thay đổi kích thước bằng Pillow, lưu trữ bằng SQLAlchemy, phục vụ bằng Flask

In lại 作者:行者123 更新时间:2023-11-29 13:18:49 32 4
mua khóa gpt4 Nike

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

服务处理程序:

@app.route('/photo/')
def uploaded_photo(filename):
thử:
photo = db_session.query(Photo)\
.filter_by(filename=filename).one()
ngoại trừ:
return 'There was an error with the photo.'

return send_file(
io.BytesIO(photo.image_blob),
attachment_filename=photo.filename,
mimetype='image/jpeg')

我如何使用 Pillow 存储图像。

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")

当您加载图像 url/photo/时会发生什么。出现一张空白照片。但是,上传部分进行得很顺利。

1 Câu trả lời

不要将图像存储在数据库中,而是使用可以保存文件的静态文件夹。只需将文档名称和详细信息保存在数据库中以供文件检索。您可以查看此文档。 liên kết用于上传……这里用于下载……liên kết

关于python - 处理文件上传,使用 Pillow 调整大小,使用 SQLAlchemy 存储,使用 Flask 提供文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45229198/

32 4 0
行者123
Hồ sơ cá nhân

Tôi là một lập trình viên xuất sắc, rất giỏi!

Nhận phiếu giảm giá Didi Taxi miễn phí
Mã giảm giá Didi Taxi
Giấy chứng nhận ICP Bắc Kinh số 000000
Hợp tác quảng cáo: 1813099741@qq.com 6ren.com