First, consider whether you really need a separate upload folder for each 
vertexID. The DAL already generates a unique filename for each upload, and 
the filename is stored in the database record, so you can always find the 
file by looking up the record.

If you need the separate folders, though, consider setting the upload 
folder with the model definition so you don't need to specify it separately 
in the SQLFORM action and so it will be easier to make use of the built-in 
response.download function:

IMAGE_FOLDER = os.path.join('/', 'Users', 'me', 'Documents')

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

Note, the folder path will end with "vertexIDNone" when there is no 
session.vertexID, but presumably that will not be an issue, as you should 
not be doing any uploads/downloads when there is no session.vertexID.

With the above change, you can then change the download function to the 
standard:

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

Anthony

On Monday, January 15, 2018 at 4:41:58 AM UTC-5, Annet wrote:
>
> Hi Anthony,
>
> Thanks for your reply.
>
> After reading the parts on download() in chapter 7 and 9 I wrote the 
> following functions
> to run locally:
>
> def logo():
>     vertexID = session.vertexID
>
>     table.vertexID.default = vertexID
>     folder = 'vertexID' + str(vertexID)
>     table.image.uploadfolder = os.path.join('/Users/me/Documents/' + 
> folder)
>
>     record = table(vertexID=vertexID)
>
>     form = SQLFORM(table, record, deletable=True, showid=False,
>                    upload=URL('image_download', args=request.vars.image))
>
>     if form.process().accepted:
>         if record and form.vars.image__delete:
>             file = record.image
>             os.remove(os.path.join('/Users/me/Documents/' + folder, file))
>     elif form.errors:
>         get_flash(response, None, 'danger', 'error')
>     elif not response.flash:
>         get_flash(response, None, 'info', 'custom')
>
>     return dict(form=form)
>
> def image_download():
>     folder = 'vertexID' + str(session.vertexID)
>     file_path = os.path.join('/Users/me/Documents/' + folder + '/')
>     return response.stream(open(file_path + request.args(0)))
>
>
> Are there any improvements to make to this code?
>
>
> Kind regards,
>
> Annet
>
>

-- 
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 the Google Groups 
"web2py-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
For more options, visit https://groups.google.com/d/optout.

Reply via email to