mod_python and storing binary form data

Discussion in 'Python' started by canistel@gmail.com, Apr 10, 2008.

  1. Guest

    Hi, I have a little python webservice that I created, and in one of
    the methods I need to store some binary data that was "posted"... I
    want to do something like this, but it doesn't work.

    username = form.get("username", "")
    message = form.get("message", "")
    attachment = form.get("attachment", None)
    ....
    c.execute("""INSERT INTO Message (username, message, attachment)
    VALUES (%s, %s, %s)""", (username, message, attachment))


    "attachment" is then some binary data that was posted, but when I look
    in the mysql database, I see values for the attachment field which
    look like:

    Field('attachment', '\x89PNG\r\n\x1a\n\x00\x00\...

    so it is storing something, just not my binary data (in this case a
    picture). the attachment column is a longblob in mysql.
     
    , Apr 10, 2008
    #1
    1. Advertising

  2. En Thu, 10 Apr 2008 15:43:23 -0300, <> escribió:

    > Hi, I have a little python webservice that I created, and in one of
    > the methods I need to store some binary data that was "posted"... I
    > want to do something like this, but it doesn't work.
    >
    > username = form.get("username", "")
    > message = form.get("message", "")
    > attachment = form.get("attachment", None)
    > ...
    > c.execute("""INSERT INTO Message (username, message, attachment)
    > VALUES (%s, %s, %s)""", (username, message, attachment))
    >
    >
    > "attachment" is then some binary data that was posted, but when I look
    > in the mysql database, I see values for the attachment field which
    > look like:
    >
    > Field('attachment', '\x89PNG\r\n\x1a\n\x00\x00\...
    >
    > so it is storing something, just not my binary data (in this case a
    > picture). the attachment column is a longblob in mysql.


    I assume you're using mod_python.util.FieldStorage.
    For normal (string) values, form.get returns a somewhat bizarre
    StringField that's a subclass of str itself. For files, it returns a Field
    object; it has a "file" attribute that you can use.
    See
    http://www.modpython.org/live/current/doc-html/pyapi-util-fstor-fld.html

    --
    Gabriel Genellina
     
    Gabriel Genellina, Apr 11, 2008
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Michael Bredbury

    Storing binary data in a web page

    Michael Bredbury, Nov 5, 2003, in forum: ASP .Net
    Replies:
    0
    Views:
    451
    Michael Bredbury
    Nov 5, 2003
  2. Channing Jones
    Replies:
    0
    Views:
    1,395
    Channing Jones
    May 23, 2005
  3. blbmdsmith
    Replies:
    1
    Views:
    660
    Graham Dumpleton
    Dec 14, 2006
  4. vj
    Replies:
    10
    Views:
    1,838
    Chris Uppal
    Mar 30, 2007
  5. darren
    Replies:
    2
    Views:
    375
    James Kanze
    Jul 5, 2008
Loading...

Share This Page