1

I'm trying to upload an attachment/png image to an already existing document on couchdb using python & flask and using a plugin called couchdb from (https://pythonhosted.org/CouchDB/getting-started.html)

I am able to get the attachment to upload, but the attachment only shows up as a small blank image.

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

    doc = db.get('andrew')
    print("doc =", doc)
db.put_attachment(doc, file, memeName, content_type="image/png")

Here is the error that Im getting: enter image description here

Andrew Irwin
  • 691
  • 12
  • 40
  • When you say "the attachment only shows up as a small blank image" -- shows up where? When you browse the database in Futon, what do you see? Is the image there? – Jonathan Hall Apr 13 '17 at 03:28
  • Hi @Flimzy, thank you for helping again. I have a link here to 2 screenshots one showing my couchdb document json and the other showing what I see when I click to view one of the attachments. Im also using qqq.png as the testing example for this question, it uploads as an attachment to couch db but when I go to view its just a blank small square. https://drive.google.com/drive/folders/0B29noplMMhOYNzE2X3E2NS1tcm8?usp=sharing – Andrew Irwin Apr 13 '17 at 09:04
  • The error in the traceback has nothing to do with couch. Flask always expects a post request to return something, usually a dictionary to pass to a template. – sarwar Apr 13 '17 at 17:21

1 Answers1

0

I'm not sure there's quite enough to go on here, but I'll take a whack. A couple questions up front in case I my intuition is incorrect.

  • What data is passed in request.form['data']? (FYI, you should avoid using 'file' as a variable name in Python as it's a builtin in and may cause some undesired behaviors)

  • Is an attachment being added to the document at all? (you can look in futon at your doc and see it there's an attachment and if so, of what size)

  • If the attachment is being saved, how are you trying to display it afterwards that results in a blank square?

I'll assume you're posting the image data, but aren't able to save it as an attachment. From the docs for the Python couchdb module,

put_attachment(doc, content, filename, content_type)

expects particular types ofcontent:

content – the content to upload, either a file-like object or a string

I assume form['data'] isn't a file like object since it's being passed in a form. If it's a .png that you're passing as text, you should probably encode it as base64 before putting it into couch. Assuming the post works properly and you can save your image into couch, you should see an attachment of the proper size in futon. You can then render your image from base64 simply as shown in this question. If the attachment isn't the size you expect, check to make sure you're actually posting what you think you are.

If you plan on having futon render the image, you may need to give it a more specific content type, since what you passed isn't a normal png but a base64 encoding of a png. Changing content type to

data:image/png;base64

may address the issue.

Community
  • 1
  • 1
sarwar
  • 2,835
  • 1
  • 26
  • 30
  • Hi @sarwar, thanks for your help. I have a link here to 2 screenshots one showing my couchdb document json and the other showing what I see when I click to view one of the attachments. Im also using qqq.png as the testing example for this question, it uploads as an attachment to couch db but when I go to view its just a blank small square. https://drive.google.com/drive/folders/0B29noplMMhOYNzE2X3E2NS1tcm8?usp=sharing – Andrew Irwin Apr 13 '17 at 09:05
  • To answer your questions. 1. base 64 encode image (long string of random characters) is passed through. 2.Yes, the attachment is being added successfully but when I go to view its just a small blank square (view link with screenshots above). 3. Im checking the attachment in the futon by clicking view attachments. i get upto to part 3 above^ and get stuck their. – Andrew Irwin Apr 13 '17 at 09:13
  • That's an excellent start. Try submitting it with the content_type in my updated answer. – sarwar Apr 13 '17 at 09:37