I'm new to Python and web2py and am currently facing a strange problem. I have images (pictures) stored in my database (SQL Server) which I'm trying to display in my website. I have managed to access the database and bring the information to the web page, but I'm having trouble with the pictures.
Finally after long time trying, I was able to pinpoint my problem, as can be seen in the following code of my "Download" function. If I try to read the image from the database and return it, the web browser just gets stuck and shows nothing. If I save the image to a temporary static file, then read it and THEN return the result, everything works fine.
My only guess is that it has something to do with scope of the variables, but I'm not sure. Here's my code:
def download():
cursor = erpDbCnxn.cursor()
cursor.execute("SELECT Picture FROM KMS_Parts WHERE PartNo = '%s'" % request.args(0))
row = cursor.fetchone()
if row:
dbpic = row.Picture
f = open ("C:/Users/Udi/Desktop/tmp.jpg", "wb") ##This is just for the test
f.write(dbpic) ##This is just for the test
f.close() ##This is just for the test
f = open ("C:/Users/Udi/Desktop/tmp.jpg", "rb") ##This is just for the test
filepic = f.read() ##This is just for the test
f.close() ##This is just for the test
return filepic ##Returning dbpic doesn't work
else:
return
Just for whoever wants to know the final result: Looks like this was resolved in one of the following versions of web2py. Currently I'm just doing "return dbpic" and it works fine.