Upload images to folder outside web2py

61 views
Skip to first unread message

Annet

unread,
Nov 2, 2020, 10:17:48 AM11/2/20
to web2py-users

To upload images to separate folders outside web2py I set the upload folder with the model definition:

UPLOADSDIR = '/home/me/apps/static/'

db
.define_table('mytable',
   
Field('image', 'upload', autodelete=True, uploadfolder=os.path.join(UPLOADSDIR, 'vertexID%s' % session.vertexID),
   
...)


I use the standard download function:

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


The form:

form = SQLFORM(table, row, deletable=True, upload=URL('default', 'download'), showid=False, buttons=buttons)
    if hasattr(request.vars.image, 'filename'):
        form.vars.imagefilename = request.vars.image.filename


and to process the form:

if form.process().accepted:
        if row and form.vars.image__delete:
            file = row.image
            os.remove(os.path.join(UPLOADSDIR, 'vertex%s' % session.back_end_vertexid, file))


In web2py 2.17.2 this all worked without problem, however, after upgrading to
web2py 2.20 it no longer works. The image doesn't show in the form, it's marked
with a broken link icon, and when I click the file link I get a page not found error.
Also when I upload a new image, the old one isn't being deleted, so the images pile
up in the folder.

I hope someone knows how to solve this issue.


Kind regards,

Annet

Annet

unread,
Nov 2, 2020, 10:35:45 AM11/2/20
to web2py-users
I forgot to say, uploading an image works, if the folder isn't
there it is being created, otherwise the image is uploaded
to the folder.

Best,

Annet

Op maandag 2 november 2020 om 16:17:48 UTC+1 schreef Annet:

Dave S

unread,
Nov 7, 2020, 5:45:08 PM11/7/20
to web2py-users


On Monday, November 2, 2020 at 7:35:45 AM UTC-8, Annet wrote:
I forgot to say, uploading an image works, if the folder isn't
there it is being created, otherwise the image is uploaded
to the folder.

Best,

Annet


Is this related to slashes being dropped, as in Gael's question?
<URL:https://groups.google.com/d/msg/web2py/b5mApXO-9Io/FICcx2JlBgAJ>

/dps

Annet

unread,
Nov 9, 2020, 4:45:06 AM11/9/20
to web2py-users
Thanks for the link.

I had a look at Gael's post, my problem isn't related to that one.

However, reading Gael's post I realised that bewteen versions
I introduced conditional models. Since the download function
is in the default controller and the uploadfolder is set in the
model I needed to add the respective model files to response.models_to_run

So, Gael's post indirectly helped me solve the issue.

Kind regards,

Annet

Op zaterdag 7 november 2020 om 23:45:08 UTC+1 schreef snide...@gmail.com:
Reply all
Reply to author
Forward
0 new messages