String Literal to Blob

Steve Holden steve at holdenweb.com
Tue Apr 8 12:15:51 EDT 2008


Victor Subervi wrote:
> Hi:
> I am able (finally) to upload an image to the database. However, when I 
> try to retrieve it, I get a string literal. Here is my code:
>  
> #!/usr/local/bin/python
> import cgitb; cgitb.enable()
> import MySQLdb
> def test():
>   host = 'mysqldb2.ehost-services.com <http://mysqldb2.ehost-services.com>'
>   user = 'user'
>   passwd = 'pass'
>   db = 'bre'
>   db = MySQLdb.connect(host=host, user=user, passwd=passwd, db=db)
>   cursor= db.cursor()
>   cursor.execute('select pic1 from products where id="3";')
>   content = cursor.fetchall()
> #  print 'Content-Type: image/jpeg\r\nContent-Length: %d\n' % len(content)
>   print 'Content-Type: image/jpeg\r\n'
>   print '<html><body>\n'
>   print content
>   print '</body><html>\n'
>   cursor.close()
>  
> test()
> (Apparently, Plesk doesn´t like if __name__ == '__main__': )
> The commented out line gives me a leading less than sign...and that´s 
> it. What do?
> TIA,
> Victor
> 
Your headers indicate you intend to serve a JPEG image, so you should 
*not* then include HTML. Take a look at the HTML of a web page with an 
image inside it (look for the <IMG src="..."> tag) and you will see that 
HTML pages reference images as separate web resources.

Thus once you have printed out your HTML headers you should them 
immediately send the contents of the database column.

regards
  Steve
-- 
Steve Holden        +1 571 484 6266   +1 800 494 3119
Holden Web LLC              http://www.holdenweb.com/




More information about the Python-list mailing list