Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8599677
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T01:26:53+00:00 2026-06-12T01:26:53+00:00

I’ve recently jumped into Web2py framework which I find very good. However, I’m now

  • 0

I’ve recently jumped into Web2py framework which I find very good. However, I’m now stuck with a ‘basic’ problem.

Context

The website I’m building is an interface for scientific code: people fills-in a form and submit it. The data (written in a file inside a shared folder) is then being processed by the code, running as a daemon in the background (the code has no link with the website, excepts this shared folder and a JSONRPC link between code->website).
The code generates files and images that I would like to make available to the website users (ie. returning them back the results they asked for).
When a job is finished, the code ‘push’ the results to the website using a JSONRPC service link, which works perfectly. The results are in a folder with a generated name which have basically the following structure:

unique_folder_name/
        file1.txt
        file2.csv
        file3.xls
        image1.png
        image2.png

The current implementation (not working properly)

Currently I have 3 DBs:

# Job database
db.define_table("job",
    Field('owner', 'string', length=60, required=True, writable=True, readable=True),
    Field('uniq_id', "string", length=60, required=True, unique=True, writable=False, readable=True))

# Result database
db.define_table("result",
    Field("job", "reference job"),
    Field("x", "integer", default=1),
    Field("y", "integer", default=0),
    Field("z", "integer", default=0),
    Field("data", "string", length=500),
    Field("message", "string", length=200))

# File database
db.define_table("file",
    Field("job", "reference job"),
    Field("file", "upload"),
    Field("isimage", "boolean", default=False))

When the code ‘push’ the results, some modules in Web2py create the entry in the ‘result’ db, and entries in the ‘files’ db associated to the job.
Since I did not find a way to make files (already on the filesystem) available to Web2py without it copying to the upload folder, I currently store the files like that (in a module):

stream = open(os.path.join(directory, _file), 'rb')
current.db.file.insert(job=jid, file=current.db.file.file.store(stream, _file), isimage=isimg)

Then, when I want to create a view with the images, I do (in a module):

rows = current.db((current.db.file.job==jid) & (current.db.file.isimage==True)).select()
for row in rows:
    div.append(I(_src=URL(c="default", f="download", args=os.path.join(directory, row.file)), _width="500", _height="500"))

and in the view: `{{=div}}“

Problems

This is just not working… The source code of the displayed page is like:

<i height="500" src="/mycode/default/download//path/to/directory/file.file.9b7d3a0367de0843.6d732d72732e706e67.png" width="500"></i>

If I type this URL in the address bar, the file download properly, but otherwise the image is not displayed on the webpage.
Also, I have to give the path of the file, even if Web2py did copy the file into ‘upload’ folder (with its new safe ugly name:)), otherwise the link is just not working.
So: no image displayed, plus the files are copied into ‘upload’ folder anyway 🙁

I’m lost, and dont see how to fix this. (I’ve tried also to add the request object when building the image URL, and also tried a custom download function… none worked so far).

EDIT (SOLUTION)

Well, there is an obvious bug in my code that I’ve missed: images tag helper is not I, but IMG 🙂 Simple mistake arising from the confusion with the I tag used in twitter bootstrap for icons… So that solves the display issue.
For streaming files without copying them in the upload folder (ie. not using an upload field in the DB), rochacbruno (many thanks to him) has put me on the right track. See my own answer for the complete solution.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T01:26:54+00:00Added an answer on June 12, 2026 at 1:26 am

    So here is the complete solution to my problem.

    In the db.py file, I’ve replaced the ‘file’ table by this definition (so files stay where they are, and are not copied into the web2py upload folder):

    # File database
    db.define_table("file",
        Field("job", "reference job"),
        Field("name", "string", length=30, required=True),   # stores the filename (without path)
        Field("isimage", "boolean", default=False))
    

    Then, in a controller (eg. ‘mycontroller’), I’ve defined this function to stream files:

    from mymodule import OUTPUT  # this is the base directory of the files
    from gluon.contenttype import contenttype
    
    def export():
        # allow to download files
        jid = request.args(0)
        fid = request.args(1)
    
        if None in (jid, fid):
            res = 'Invalid URL'
        else:
            # get the file row
            row = db.file(fid)
    
            # some checks (not necessary if you know what you're doing)
            jrow = db.job(jid)
            if row is None:
                res = "unknown file ID"
            elif jrow.id  is None:
                res = "unknown job ID"
            else:
                filename = row.name
                # jrow.perma_id, is a field in the 'job' DB, that I use to create a unique
                # directory name, so the files of job ID 'jid' are under: OUTPUT/perma_id/
                fullname = os.path.join(OUTPUT, jrow.perma_id, filename)
    
                ext = os.path.splitext(filename)[1]
                response.headers['Content-Type'] = contenttype(ext)
                response.headers['Content-disposition'] = 'attachment; filename=%s' % filename
                res = response.stream(open(fullname, "rb"), chunk_size=4096)
    
        return res
    

    Please note, that I ran into another problem at this point: I first thought to pass the full path as a request argument (eg. URL(c='mycontroller', f='export', args=(path, filename))),
    but it did not work since path was containing ‘/’ that were split into as many arguments…
    If you don’t have an easy path like me (ie. just one component change), you can store the path into the ‘file’ DB for instance.

    Then, for the view (using either a module or whatever you like):

    rows = current.db((current.db.file.job==jid) & (current.db.file.isimage==True)).select()
    for row in rows:
        div.append(IMG(_src=URL(c="mycontroller", f="export", args=(jid, fid), _width="500", _height="500"))
    

    Notice that the I helper tag has been replaced by the correct IMG one. ‘jid’ is the job ID, and ‘fid’ is the file ID you want to display/download.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
this is what i have right now Drawing an RSS feed into the php,
I am currently running into a problem where an element is coming back from
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I am trying to understand how to use SyndicationItem to display feed which is
I used javascript for loading a picture on my website depending on which small
I have a jquery bug and I've been looking for hours now, I can't
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have a French site that I want to parse, but am running into
I would like to run a str_replace or preg_replace which looks for certain words

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.