Serving images from the "uploads" folder

22 views
Skip to first unread message

voltron

unread,
May 27, 2008, 12:58:21 PM5/27/08
to web2py Web Framework
How does one serve images from the uploads folder? This seems to have
no effect:

avatar = IMG(_src=URL(a = 'init', c= 'uploads', f =
present_user.avatar))

The HTML is fine, but the image is not served by the server, security
mechanism?

Chris Leonello

unread,
May 27, 2008, 2:13:47 PM5/27/08
to web2py Web Framework
Yes, I believe the browser cannot directly access the /uploads/ url.

Try using a controller method to fetch and stream back the file. Here
is an example where I store the file's filename on disk as well as
content-type and human readable file name in a database table, then
return the appropriate file, content-type, and user readable filename
when requested:

def download():
import os
fid = request.args[0]
f = db(db.file.id==fid).select()[0]

filename=os.path.join(request.folder,'uploads/','%s' % f.file)
ret = open(filename, 'rb')
response.headers['Content-Type'] = f.content_type
response.headers['Content-Disposition'] = 'inline; filename="%s"'
% f.name
return ret.read()

So, calling this function like:

www.somecompany.com/myapp/mycontroller/download/27

will download the file with id of 27.

There is another example in the earlier docs for downloading images.
If you are using the default web2py naming scheme and upload
implementation, this method is nice. I needed to be able to have
arbitrary user file names associated with the files, so I developed my
own method.

Chris Leonello

unread,
May 27, 2008, 2:20:32 PM5/27/08
to web2py Web Framework
If your using the default web2py file upload/download functionality,
there are a few examples on the faq. Here is one:

http://mdp.cti.depaul.edu/AlterEgo/default/show/50

Again, you need to use a controller function to do the actual
download, not link directly to the file.

voltron

unread,
May 27, 2008, 4:08:10 PM5/27/08
to web2py Web Framework
Thanks for your reply Chris. So I would have to actually have to
trigger a stream to show up an image in the HTML file? I am not
offfering images for users to download, they are just avatar images to
be displayed.

Thanks

mdipierro

unread,
May 27, 2008, 4:27:10 PM5/27/08
to web2py Web Framework
If you have this in a view:

{{IMG(_src=URL(r = 'request', f= 'giveme',
args=[present_user.avatar]))}}

you need the associated controller

def giveme(): return
response.stream(os.path.join(request.folder,'uploads',request.args[0]))

(assuming presnt_user.avatar contains a valid filename in uploads)
response stream will set the content-type, stream the file if it is
large and deal with IF_MODIFIED_SINCE and PARTIAL REQUEST. This is not
automatic because the developer may want to stick some authorization
in there.

Massimo

voltron

unread,
May 27, 2008, 4:38:49 PM5/27/08
to web2py Web Framework
Thanks Massimo,but I am still having problems, this is rendered in the
HTML

<img src="/init/accounts/giveme/user_profiles.avatar.32798039028.jpg"/
> ( I have the function in my accounts controller)

The path is correct, the image exits, but it is not rendered in the
HTML, is there something else I have to take notice of?


Thanks

Massimo Di Pierro

unread,
May 27, 2008, 6:08:45 PM5/27/08
to web...@googlegroups.com
They visit the url

http://127.0.0.1:8000/init/accounts/giveme/user_profiles.avatar.32798039028.jpg

and see if you can download it or not. 

Try print the path as computed by os.path.join

Massimo

On May 27, 2008, at 3:38 PM, voltron wrote:

/init/accounts/giveme/user_profiles.avatar.32798039028.jpg


Massimo Di Pierro

unread,
May 27, 2008, 6:10:34 PM5/27/08
to web...@googlegroups.com
and I am assuming you stored the images on the filesystem and not in the database....

voltron

unread,
May 28, 2008, 2:35:54 AM5/28/08
to web2py Web Framework
1. I am using SQLFORM as in the examples
2. I am not storing or modifying the files in any way
3. I can see the files in the upload folder, they are there so the
file uploading works
4. I am not providing a way for the users to download files, they
should only be displayed in the HTML. Every user should be able to
upload a file for her or his use as an avatar

The computed file is correct, as I have stated a few posts above, this
is the output of what gets generated in the HTMP template:

<img src="/init/accounts/giveme/user_profiles.avatar.32798039028.jpg"/
>

The link is correct, but the files are not shown






On May 28, 12:10 am, Massimo Di Pierro <mdipie...@cs.depaul.edu>
wrote:

Massimo Di Pierro

unread,
May 28, 2008, 3:08:00 AM5/28/08
to web...@googlegroups.com
For this to work

<img src="/init/accounts/giveme/user_profiles.avatar.32798039028.jpg"/>

you need to allow he client to download

http://127.0.0.1:8000/init/accounts/giveme/user_profiles.avatar.
32798039028.jpg

If this does not work there are many possibilities:
- the file is/gets corrupted
- the controller does not work
- the html in the view has a bug
hard to say without looking at the code.

This is why you need to test the different parts separately.

My suggestion is bypass the view and try download the file by calling
the URL above. Can you download the file? Can you see the image?

GA

unread,
May 28, 2008, 8:09:44 AM5/28/08
to web2py Web Framework
In my site I'm using this solution:

filename=os.path.join(request.application, 'appadmin/download', '%s' %
Image)

<img src="/{{=filename}}">


I hope it helps.

bye



mdipierro

unread,
May 28, 2008, 8:12:01 AM5/28/08
to web2py Web Framework
This only works if you are logged in into appadmin. You are supposed
to copy the download function from appadmin into default.py if you
want to expose it without authorization.

GA

unread,
May 28, 2008, 8:23:13 AM5/28/08
to web2py Web Framework
>This only works if you are logged in into appadmin. You are supposed
>to copy the download function from appadmin into default.py if you
>want to expose it without authorization.

i have sent the old version, sorry

after copy download function from appadmin

filename=os.path.join(request.application, 'your_controller/download',
'%s' % Image

bye.

voltron

unread,
May 28, 2008, 1:35:53 PM5/28/08
to web2py Web Framework
Thank you all for your help. I found out that I had to import "os" I
really wonder why I had no traceback about this with my other tries.
Reply all
Reply to author
Forward
0 new messages