将图像附件添加到现有文档

时间:2017-03-13 21:09:03

标签: python image http flask couchdb

我有一个带有图片附件的文档。我希望在文档中添加另一个图像,以便文档可以保存多个附件,即图像,以前现有的图像附件不会丢失。

我可以将图像上传到现有文档,但它会删除已存在的图像。我不想要这个。我想添加新图像并保持旧图像,我希望能够拥有包含多个图像的文档。我知道我有rev_id硬编码,但它用于测试。

@app.route('/process', methods=['POST'])
def process():
if request.method == 'POST':

    memeName = request.form['memeName']
    memeBaseName = request.form['memeBaseName']
    file = request.form['data']

    if file:
        print("file: ", file)
        memeName += ".png"
        print("memeName: ", memeName)
        print("memeBaseName: ", memeBaseName)

        atachmentJson = {
            "_id": memeBaseName,
            "_rev": "5-60b616d491050e36302797eb126dcfa8",
            "_attachments":
                {
                    memeName:
                        {
                            "content_type": "image/png",
                            "data": file
                        }
                }
        }
        uploadRequest = requests.post('http://127.0.0.1:5984/' + database + '/', data=None, json=atachmentJson)

        print("uploadRequest: ", uploadRequest.status_code)

        if uploadRequest.status_code == requests.codes.ok:
            return jsonify({'name': "Meme Uploaded"})

        elif uploadRequest.status_code == requests.codes.created:
            return jsonify({'name': "Meme Uploaded"})

        elif uploadRequest.status_code == requests.codes.accepted:
            return jsonify({'name': "Meme Uploaded"})

        else:
            return jsonify({'error': "Failed to Upload Meme. Sorry :("})

0 个答案:

没有答案