如何使用 flask 上传图像并存储在 couchdb 中?

How to upload an image with flask and store in couchdb?

之前的一个 询问如何从 couchdb 中检索附件并将其显示在烧瓶应用程序中。

这个问题问的是如何执行相反的操作,即如何使用 flask 上传图像并保存为 couchdb 附件。

看看WTF中的例子:

from werkzeug.utils import secure_filename
from flask_wtf.file import FileField

class PhotoForm(FlaskForm):
    photo = FileField('Your photo')

@app.route('/upload/', methods=('GET', 'POST'))
def upload():
    form = PhotoForm()
    if form.validate_on_submit():
        filename = secure_filename(form.photo.data.filename)
        form.photo.data.save('uploads/' + filename)
    else:
        filename = None
    return render_template('upload.html', form=form, filename=filename)

看看FileField api docs. There you have a stream method giving you access to the uploaded data. Instead of using the save method as in the example you can access the bytes from the stream, base64 encode it and save as an attachment in couchdb, e.g. Using put_attachment。或者,FileStorage api 文档建议您可以使用 read() 检索数据。