putting Image from database in cgi app

Gerhard Häring gh at ghaering.de
Wed May 21 07:14:39 EDT 2003


vivek kumar wrote:
> Hi all,
> 
>  I was wondering if there is any way to show the file read from database 
> in the cgi app..
> ie. When I read an image from database it is read in a buffer. So, 
> currently what I am doing is creating a temporary file, writing the 
> buffer in that file and showing that file in the cgi app something like:
> 
>   cur.execute("select pic from pictest where id=1")
>   rst=cur.fetchone()
>   s=open("temp.jpg","wb")
>   s.write(rst[0])
>   s.close
>   print '<img src="temp.jpg">'
> 
> So, I was wondering if there is any way in which I can show the file 
> directly without creating an intermediate file???

Sure, but one HTTP request can only deliver one resource to the client, 
be it a HTML document or a JPEG image.

A CGI script that would deliver a JPEG image to the client could look 
like this:

#v+
...
cu = cx.cursor()
cu.execute("SELECT PIC FROM PICTEST WHERE ID=1")
img = cu.fetchone()[0]

print "Content-type: image/jpeg\n"
print img

cx.close()
#v+

-- Gerhard





More information about the Python-list mailing list