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

南楼画角 提交于 2019-12-23 05:48:04

问题


A previous question asks how to retrieve at attachment from couchdb and display it in a flask application.

This question asks how to perform the opposite, i.e. how can an image be uploaded using flask and saved as a couchdb attachment.


回答1:


Take a look at the example from 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)

Take a look at the 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. Alternatively, the FileStorage api docs suggest you can use read() to retrieve the data.



来源:https://stackoverflow.com/questions/40852404/how-to-upload-an-image-with-flask-and-store-in-couchdb

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!