manually saving files

34 views
Skip to first unread message

weheh

unread,
Jan 7, 2010, 2:37:54 AM1/7/10
to web2py-users
# model
db.define_table('doc',
Field('title','string'),
Field('filename','upload')
)

#controller
text_form=SQLFORM.factory(Field('text_in','text',db.doc))
file_form=SQLFORM.factory(db.doc)
...
if text_form.accepts(request.vars,formname='text_form'):
#insert data into the doc table
...
if file_form.accepts(request.vars,formname='file_form'):
# etc

The view has custom forms for both text_form and file_form.

The question is ... how to properly insert data into the doc table in
the case of the text_form? text_form.vars.text_in has the results of
textarea form field. These data are easily stored in an uploads/
file.txt file. The question is how to make the db.doc.filename field
point to the uploads/file.txt file such that it will be consistent
with a true file upload vs. a text field upload? Just inserting the
name of the uploads/file.txt into the db.doc.filename field isn't the
right way. (Hope this is clear enough.)

mdipierro

unread,
Jan 7, 2010, 8:54:01 AM1/7/10
to web2py-users
If you have an <input type="file" name="xyz"/> represented as a
FieldStorage in request.vars.xyz you can do

db.doc.insert(filename=db.doc.filename.store
(request.vars.xyz.file,request.vars.xyz.filename))

weheh

unread,
Jan 7, 2010, 5:36:58 PM1/7/10
to web2py-users
How about with an <input type='textarea' name='xyz' /> which I then
write to a file abc under uploads/folder1/folder2 and want to insert
into the db.doc.filename, which is of type 'upload'?

> > right way. (Hope this is clear enough.)- Hide quoted text -
>
> - Show quoted text -

mdipierro

unread,
Jan 7, 2010, 5:53:22 PM1/7/10
to web2py-users
There are two subfolders: private and uploads

You should not mess up with uploads. Let SQLFORM put staff there and
download retrieve it. You can manually do it but ONLY if you use

db.table.insert(fieldname=db.table.fieldname.store
(stream,'filename'))

This is very special folder because there are a lot of security
implications in having users uploads data (directory traversal
attacks) and download data (authorizations). Let web2py deal with it
by the book. It is complicated. There is a lot of code in web2py just
to deal with this.

Files that public should go in static/.

Everything else should go in private but now you are on your own. You
decide file naming conventions but you are also responsible for
security holes.

Massimo

Thadeus Burgess

unread,
Jan 7, 2010, 5:59:06 PM1/7/10
to web...@googlegroups.com
There is an issue with the uploads folder.

Most filesystems will suffer a huge performance penaltiy if there are
> 100 files in a single folder.

So in an app that receives alot of uploads, having these all in one
folder is really really bad for the servers performance (think flickr
type site).

How could web2py handle a subfolder structure, that put the files that
were uploaded by date/time etc, something that keeps more than 100
files in any given folder ?

-Thadeus

> --
> You received this message because you are subscribed to the Google Groups "web2py-users" group.
> To post to this group, send email to web...@googlegroups.com.
> To unsubscribe from this group, send email to web2py+un...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/web2py?hl=en.
>
>
>
>

mdipierro

unread,
Jan 7, 2010, 6:04:57 PM1/7/10
to web2py-users
You can have subfolder structure

Field('file','upload',uploadfolder='....')

any field can have its own uploadfolder which can me (but does not
need to be) a subfolder of uplaods/

On Jan 7, 4:59 pm, Thadeus Burgess <thade...@thadeusb.com> wrote:
> There is an issue with the uploads folder.
>
> Most filesystems will suffer a huge performance penaltiy if there are
>
> > 100 files in a single folder.
>
> So in an app that receives alot of uploads, having these all in one
> folder is really really bad for the servers performance (think flickr
> type site).
>
> How could web2py handle a subfolder structure, that put the files that
> were uploaded by date/time etc, something that keeps more than 100
> files in any given folder ?
>
> -Thadeus
>

weheh

unread,
Jan 7, 2010, 7:55:53 PM1/7/10
to web2py-users
I assume I have to write the logic to determine whether uploadfolder
exists before writing into it, correct?

> > > For more options, visit this group athttp://groups.google.com/group/web2py?hl=en.- Hide quoted text -

mdipierro

unread,
Jan 7, 2010, 7:58:31 PM1/7/10
to web2py-users
true

> > > > For more options, visit this group athttp://groups.google.com/group/web2py?hl=en.-Hide quoted text -

Message has been deleted
Message has been deleted

weheh

unread,
Jan 8, 2010, 4:31:07 PM1/8/10
to web2py-users
Massimo, thanks for your continued support on this issue. I'm still
having trouble.

I'm doing this to store an uploaded file:

db.doc.insert(filename=db.doc.filename.store
(request.vars.filename.file,request.vars.filename.filename))

where doc is a table with ... Field('filename','upload') ...

Problem is, I get a file with name no_table.filename.crypto-string.txt
and doc.filename.crypto-string.txt, where the doc.filename.crypt-
string.txt is empty and the no_table.filename.crypto-string.txt has my
uploaded text file. So this obviously isn't working. What do you think
I'm doing wrong?

mdipierro

unread,
Jan 8, 2010, 4:43:04 PM1/8/10
to web2py-users
I need to look at the complete action. I suspect you have
SQLFORM.factory somewhere that is interefring with the process.
Reply all
Reply to author
Forward
0 new messages