Problem Displaying image in View

33 views
Skip to first unread message

Yannick

unread,
Jun 21, 2009, 12:00:44 PM6/21/09
to web2py Web Framework
Hello Mate,
I seem to have a problem to upload Image file in the view... Here is
an example of how I implement it...

### In the Controller ##
def download():
import os
path=os.path.join(request.folder,'uploads',request.args[0])
return response.stream(path)

def index():
photoSet = db(db.photo.id>0).select()
return photoSet

### In the View #####
{{for photo in photoSet:}}
<img src="{{=URL(r=request,f='download',
args=photo.fileupload)}}">
{{pass}}

#########################################################################

I also follow instruction from thread like this one below and tried
using the following code for the "Download":
def download():
return response.download(request,db)

http://groups.google.com/group/web2py/browse_thread/thread/2aff11d23ddb66bb/42767626a7e6a3a8?hl=en&lnk=gst&q=Img#42767626a7e6a3a8

I keep receiving a Bad Request message "400 Bad Request" with link
like this when debugging with "Firebug" :
http://127.0.0.1:8000/BU/Action/download/FieldStorage%28%27file%27%2C%20%27models.jpg%27%2C%20%27%5Cxff%5Cxd8%5Cxff%5Cxe0%5Cx00%5Cx10JFIF%

Any idea will be must appreciated....

Thanks You

mdipierro

unread,
Jun 21, 2009, 12:28:19 PM6/21/09
to web2py Web Framework
How did you insert the images in the database? Which Python version
are you using?

It seems your fileupload field contains a serialized FieldStorage
object and not the web2py filename. This is very wrong.
The problem is not in the code you show but in the data you have in
db. Please show us more about your app, including model and how data
gets inserted.

Massimo


On Jun 21, 11:00 am, Yannick <ytchatch...@gmail.com> wrote:
> Hello Mate,
> I seem to have a problem to upload Image file in the view... Here is
> an example of how I implement it...
>
> ### In the Controller ##
> def download():
>     import os
>     path=os.path.join(request.folder,'uploads',request.args[0])
>     return response.stream(path)
>
> def index():
>      photoSet = db(db.photo.id>0).select()
>      return photoSet
>
> ### In the View #####
>   {{for photo in photoSet:}}
>       <img src="{{=URL(r=request,f='download',
> args=photo.fileupload)}}">
>   {{pass}}
>
> #########################################################################
>
> I also follow instruction from thread like this one below and tried
> using the following code for the "Download":
> def download():
>       return response.download(request,db)
>
> http://groups.google.com/group/web2py/browse_thread/thread/2aff11d23d...
>
> I keep receiving a Bad Request message "400 Bad Request"  with link
> like this when debugging with "Firebug" :http://127.0.0.1:8000/BU/Action/download/FieldStorage%28%27file%27%2C...

Yannick

unread,
Jun 21, 2009, 5:01:23 PM6/21/09
to web2py Web Framework
Thanks for your note...
Here is the code for uploading the image... I'm Using Python Version
2.5

@auth.requires_login()
def upload():
from gluon.sqlhtml import form_factory
albumNameList = [y.id for y in db(db.album.buacct_fk ==
session.auth.user.id).select(db.album.id)]
formPic = form_factory(
SQLField('photoname',label='photo'),
SQLField('fileupload',type='upload',
label='Upload'),
SQLField('Album', requires = IS_IN_SET
(albumNameList), label='Album')
)
if formPic.accepts( request.vars, session,
formname='formPic',keepvalues=True):
feedback = db.photo.insert(fileupload=request.vars.fileupload,
photoname= request.vars.photoname, albphotoref_fk= request.vars.Album)
response.flash = 'Photo stored'
elif formPic.errors: response.flash = 'Problem Uploading please
try again'

return dict(formUploadPic = formPic)


# Code to create an album
@auth.requires_login()
def createAlbum():
# create a new album photo
formCreateAlb = form_factory(
SQLField('albumname',label='Album',
requires=IS_NOT_EMPTY()),
SQLField('albumdesc',label='Description')
)
if formCreateAlb.accepts(request.vars, session,
formname='create_Album'):
feedback = db.album.insert(buacct_fk = session.auth.user.id,
albumname = request.vars.albumname, albumdesc =
request.vars.albumdesc )
return dict(form = formCreateAlb)


Thanks for your help and input on this...

Cheers,
Yannick P.

Yannick

unread,
Jun 24, 2009, 11:44:29 PM6/24/09
to web2py Web Framework
Hello mate... Here is the solution from Massimo just in case you may
have the same problem.

##############################

Replacing the center/upload function with this will solve the problem:

def upload():

from gluon.sqlhtml import form_factory

# retrieve AlbumId
# albumNameList = [(y.id , x.albumname) for (y,x) in db
(db.album.buacct_fk == session.auth.user.id).select(db.album.id,
db.album.albumname)]
albumNameList = [y.id for y in db(db.album.buacct_fk ==
session.auth.user.id).select(db.album.id)]

db.photo.photoname.label='Photo'
db.photo.fileupload.label='Upload'
db.photo.albphotoref_fk.label='Album'
db.photo.albphotoref_fk.requires = IS_IN_SET(albumNameList)
formPic = SQLFORM(db.photo, fields=
['photoname','fileupload','albphotoref_fk'])
if formPic.accepts( request.vars, session,
formname='formPic',keepvalues=True):

response.flash = 'Photo stored Fine'
elif formPic.errors: response.flash = 'Problem Uploading please try
again'

return dict(formUploadPic = formPic)



The fact is that if you use a custom form (as you did),
request.vars.photoimage would be a cgi.FieldStorage object. To handle
it manually, you would have to create a temp file, read from the
cgi.FieldStorage file stream, write in the temp file and store the
temp filename into the photoupload field in the table. And still it
would not work on GAE or systems that require storing the actual image
in the database and not on the filesystem. This mechanism is
complicated and recreating it would be a lot of work. Leave it to
web2py.


##################################

Thanks again Massimo for this,

Cheers,
Yannick P.
Reply all
Reply to author
Forward
0 new messages