Getting a uploaded file as image?

11 views
Skip to first unread message

Michael

unread,
Jun 14, 2009, 11:28:42 PM6/14/09
to web2py Web Framework
I have a model that has:

db.Field('screenshot', 'upload'))

In my controller i have:

def add():

form = SQLFORM(db.apps)

if form.accepts(request.vars, session):
redirect(URL(r=request, c='default', f='index'))

return dict(form = form)

and in my view I have:

{{for app in records:}}
<div>
<img src="{{=URL(r=request, c='uploads', f=app.screenshot)}}">
{{=A(app.name, _href=URL(r=request, f=app.link))}}
<span>by {{=app.author}}</span>
<br/>
<span>{{=app.description}}</span>
</div>
{{pass}}

this returns a broken image. anyone know how to display an image from
the upload of a form?

mdipierro

unread,
Jun 15, 2009, 1:57:22 AM6/15/09
to web2py Web Framework
I marked with ## the lines that I added, changed. Hope it is self-
explanatory.
Let me know if this fixes it.

def add():
form = SQLFORM(db.apps)
if form.accepts(request.vars, session):
redirect(URL(r=request, c='default', f='index'))
records=db(db.apps.id>0).select() ####
return dict(form = form,records=records) ###

def download(): ###
return response.download(request,db) ###

And in the view

{{for app in records:}}
<div>
<img src="{{=URL(r=request,
f='download', args=app.screenshot)}}"> ####
{{=A(app.name, _href=URL(r=request,
f=app.link))}}
<span>by {{=app.author}}</span>
<br/>
<span>{{=app.description}}</span>
</div>
{{pass}}

Michael

unread,
Jun 16, 2009, 6:47:04 PM6/16/09
to web2py Web Framework
This worked perfectly, thanks!

How would I serve up the image if I was broadcasting an XML service
API?

I want my service to respond to a request with images that can be
displayed on their website.

Thanks!

mdipierro

unread,
Jun 17, 2009, 2:07:27 AM6/17/09
to web2py Web Framework
Not sure I understand. The uploaded images have URL equal to

URL(r=request,f='download', args=app.screenshot)

you can use this to build the XML tags. Alternative you can do

db.apps.screenshot.represent=lambda value: URL(r=request,f='download',
args=value)

Massimo
Reply all
Reply to author
Forward
0 new messages