saving uploads in a place other than applications/myapp/uploads

24 views
Skip to first unread message

weheh

unread,
Sep 16, 2009, 4:52:20 PM9/16/09
to web2py-users
Has anyone successfully been able to store and retrieve uploaded files
in a place other than applications/myapp/uploads? A subdirectory under
uploads is OK. I want to store user files in applications/myapp/
uploads/userid/file.jpg or some variant of that, and then enable users
to also download their files. I believe that the download function
will need to be modified to accomplish this, but it's not clear to me
how to do it.

mdipierro

unread,
Sep 16, 2009, 5:29:25 PM9/16/09
to web2py-users
Currently the only way to achieve this is:
1) move the file after has been uploaded
2) write your own download controller so that file can be retrieved.

Richard

unread,
Sep 16, 2009, 8:24:57 PM9/16/09
to web2py-users
currently I save uploaded files to an external hard drive, and to
avoid additional I/O operations I use uploadfield=False. Then I save
the file with:

Field('model', type='upload', uploadfield=False, requires=IS_NOT_EMPTY
())
...
gluon.fileutils.copystream(request.vars.model.file, open
(model_filename, 'wb'), int(request.env.content_length))

Richard

mdipierro

unread,
Sep 16, 2009, 10:29:52 PM9/16/09
to web2py-users
Ok. try this:

db.table.field.uploadfolder=od.path.join(request.folder,"uploads/
somewhere")

reuires trunk. folder somewhere must exist.

weheh

unread,
Sep 16, 2009, 11:26:47 PM9/16/09
to web2py-users
Very cool, Massimo. Thank you for your quick response! But ...

- Shouldn't that be os.path.join?
- Should I interpret your message as meaning that this will also work
with the standard download function -- the one that does a "return
response.download(request,db)"?
- Can the db.table.field.uploadfolder be any place on the file system,
even places outside of web2py?
> > > > how to do it.- Hide quoted text -
>
> - Show quoted text -

mdipierro

unread,
Sep 16, 2009, 11:46:54 PM9/16/09
to web2py-users


On Sep 16, 10:26 pm, weheh <richard_gor...@verizon.net> wrote:
> Very cool, Massimo. Thank you for your quick response! But ...
>
> - Shouldn't that be os.path.join?
yes, sorry for the typo

> - Should I interpret your message as meaning that this will also work
> with the standard download function -- the one that does a "return
> response.download(request,db)"?
that should work. Please give it a try and let me know.

> - Can the db.table.field.uploadfolder be any place on the file system,
> even places outside of web2py?
yes.

weheh

unread,
Sep 17, 2009, 12:10:49 AM9/17/09
to web2py-users
Thanks again for doing this. I'll have to grab the latest trunk and
check it out. Will let you know.

One other thing, please. I emailed you an example where the input is a
field of type text. Once uploaded, the form.vars.text_in field is
written to a file in the "somewhere" folder. The filename of this file
was then inserted into the db.table.field.filename, where Field
('filename','upload'). When accessing this field with the download
function, however, the action failed. Is there some special method
required to do this -- read text in, write to a file, and make it
behave exactly as if the file had been uploaded using a field of type
upload?
> > > - Show quoted text -- Hide quoted text -

Richard

unread,
Sep 17, 2009, 2:07:50 AM9/17/09
to web2py-users
thanks for exposing that - now I can simplify some existing code.

Fran

unread,
Sep 18, 2009, 2:38:03 PM9/18/09
to web2py-users
On Sep 17, 3:29 am, mdipierro <mdipie...@cs.depaul.edu> wrote:
> db.table.field.uploadfolder=os.path.join(request.folder,"uploads/
> somewhere")

I've tested it & it works as-advertised, thankyou :)

A further enhancement I'd like to see is to be able to specify the
filename in the form of:
subfolder/name instead of table.field.name

This would mean that I can have a folderful of standard icons for GIS
markers all with their default names & folders - much tidier.

I see that the table.field.name construct for field.retrieve happens
in globals.py's download() & field.store happens in sqlhtml.py's
SQLFORM.accepts()

Would you accept a patch which stored in the format I specified if
db.table.field.uploadfolder is specified?
Or should we add a separate flag to make these 2 functionalities
independent?

Best Wishes,
Fran.

weheh

unread,
Sep 19, 2009, 2:39:36 PM9/19/09
to web2py-users
Massimo, I tested it on the latest release and it works. - Richard
Reply all
Reply to author
Forward
0 new messages