How do I display uploaded images from uploads folder

63 views
Skip to first unread message

Maurice Waka

unread,
Nov 11, 2019, 5:37:31 AM11/11/19
to web2py-users
I'm trying to retrieve an uploaded image from the uploads folder using this example.

I keep on being redirected back to the index page.

I also tried this option with the same result.

This is the controller code:

def showarticle():
    id
= request.args(0) or redirect(URL('default', 'index'))
    article
= Article[id]
   
try:    
       
import os
       
from PIL import Image
   
except:
       
return
    stream
=Image.open(request.folder + 'uploads/' + id)
   
return response.stream(stream, attachment=False, article=article)

How can I get this right?
Regards

Ruslan Gareev

unread,
Nov 11, 2019, 7:48:14 AM11/11/19
to web2py-users
Hi, looks like your request.args(0) is empty.

понедельник, 11 ноября 2019 г., 15:37:31 UTC+5 пользователь Maurice Waka написал:

Maurice Waka

unread,
Nov 11, 2019, 3:10:53 PM11/11/19
to web2py-users
But I did upload and the image is in the folder.

--
Resources:
- http://web2py.com
- http://web2py.com/book (Documentation)
- http://github.com/web2py/web2py (Source code)
- https://code.google.com/p/web2py/issues/list (Report Issues)
---
You received this message because you are subscribed to a topic in the Google Groups "web2py-users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/web2py/GbwQoJymKns/unsubscribe.
To unsubscribe from this group and all its topics, send an email to web2py+un...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/web2py/d9f38307-39a9-4b1b-b126-a440a8ad8d07%40googlegroups.com.

Dave S

unread,
Nov 11, 2019, 5:01:19 PM11/11/19
to web2py-users


On Monday, November 11, 2019 at 12:10:53 PM UTC-8, Maurice Waka wrote:
But I did upload and the image is in the folder.

How are you generating the URL for showarticle()?

Also, I think you can just do an ordinary file open(), rather than having to import PIL and doing an Image.open()

BTW, I handle images as <IMG> tags, where src=showimg(id), and showimg() reads the file.  I also have an example, maybe previously posted, where I use download() instead of a custom showimg(),so the controller does a select on the upload table, and then
return dict(img=row.fdata)

and the view has
{{=IMG(_src=(URL"download", img), _alt="test image")}}

Good luck!

Dave S
/dps



Maurice Waka

unread,
Nov 12, 2019, 1:39:58 AM11/12/19
to web2py-users
Thanks.
Let me work it out.
Regards

--
Resources:
- http://web2py.com
- http://web2py.com/book (Documentation)
- http://github.com/web2py/web2py (Source code)
- https://code.google.com/p/web2py/issues/list (Report Issues)
---
You received this message because you are subscribed to a topic in the Google Groups "web2py-users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/web2py/GbwQoJymKns/unsubscribe.
To unsubscribe from this group and all its topics, send an email to web2py+un...@googlegroups.com.

Dave S

unread,
Nov 12, 2019, 3:06:43 AM11/12/19
to web2py-users


On Monday, November 11, 2019 at 10:39:58 PM UTC-8, Maurice Waka wrote:
Thanks.
Let me work it out.
Regards


Also note that a file uploaded to an upload field is not stored with the name the user passed to the upload form; instead, it is stored with a mangled, er, obfuscated name, and to find the file you should expect to do a db query.  See Chapter 6 under Field Types.

/dps



Reply all
Reply to author
Forward
0 new messages