I am a web2py newbie and I have the following doubt. I have a web2py application which take some inputs from the user and then generate some images. These images are then stored inside a folder under the private folder. I want to display these images on the web2py application. How do I do that? Also, I don't want to upload the images as a database but simply read them from the folder and display it directly in the application. I got a reference from here: http://www.widecodes.com/0xmqqVkXkP/web2py-downloading-files-displaying-images.html
But, couldn't quite understand it. Any help? Thanks in advance!
Assuming you know the file name and path, you can return any file to the browser via a controller action that calls response.stream
. In a controller (eg, default.py):
import os
def serve_file():
filename = request.args(0)
path = os.path.join(request.folder, 'private', 'file_subfolder', filename)
return response.stream(path)
In a view, you would then include an image as follows:
<img src="{{=URL(default, serve_file, args=filename)}}" />
If the images are intended to be public, a better option is to store them in the /static folder -- any files in /static can simply be served directly via their URL:
<img src="{{=URL('static', 'image_files', args=filename)}}" />
This method does not require a special controller action to call response.stream
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.