Are you saying you want the name in the "filename" field to be encoded into 
the string stored in the "upload" field so the file has the name in the 
"filename" field upon download? If so, then try:

if request.vars.upload and request.vars.filename:
    request.vars.upload.filename = request.vars.filename
form = SQLFORM(db.fileobject)

That will re-assign the filename of the uploaded file to the value entered 
by the user, and web2py will then encode that user-entered filename into 
the new filename. Another option is to do nothing upon upload and instead 
replace the original filename with the user-entered filename upon download 
(which you could do via a custom_retrieve function for the "upload" field).

Note, web2py relies on the filename extension to set the HTTP headers 
appropriately upon download, so you may want to add some logic to obtain 
the original filename extension in case the user fails to enter it.

Anthony

On Friday, December 28, 2012 4:15:52 AM UTC-5, Joe  Barnhart wrote:

> I'm not sure why this is difficult, but I see many posts about this when I 
> search, yet none exactly work for me.  This seems like it should be easy 
> but is surprisingly difficult in web2py.
>
> I want to keep a table of uploaded files with the uploads in a "blob" 
> field and the names in clear text in a "filename" field.  Like this model:
>
> db.define_table("fileobject",
>     Field("filename","string",length=50,readable=False,writable=False),
>     Field("upload","upload",uploadfield="object_data"),
>     Field("object_type","string",length=20,readable=False,writable=False),
>     Field("object_data","blob"),
>     Field("owner","reference auth_user",default=auth.user_id,readable=
> False,writable=False),
>     Field("saved","datetime",default=datetime.now(),readable=False,
> writable=False),
>     Field("state","string",length=16,readable=False,writable=False),
>     migrate=settings.migrate)
>
> Reading the relevant posts and the online book lead me to believe the way 
> is with a controller like this one:
>
> def index():
>     form=SQLFORM(db.fileobject)
>     if request.vars.upload:
>         form.vars.filename = request.vars.upload.filename
>     if form.process().accepted:
>         response.flash = 'form accepted'
>     elif form.errors:
>         response.flash = 'form has errors'
>     return dict(form=form)
>
> And yet this controller does not work.  The blob is filled in but the 
> filename is ignored and shows in the table as "None".  In fact, form.vars 
> is an empty collection and placing form.vars.filename in it does not 
> produce an error, but it is ignored by the form processing.  In addition to 
> the filename, I'd like to add the information about the object type etc. at 
> the same point in the process, i.e. after the file has been chosen but 
> before it has been loaded into the database.
>
> I've been trying to scan through the code (I'm using 2.3.2 source) to 
> answer my own questions about the upload field but it's just taking too 
> much time.  The upload field is one of those parts of web2py that you 
> either love or... uh... don't.  To me it seems designed for a very specific 
> use model.  If my use model deviates too much then using "upload" is like 
> pounding square pegs into round holes.  I'm tired of pounding.  Someone 
> show me how to whittle off the corners of my square peg!
>
> (You would think this would be an easy task, yet from the number of posts 
> on this exact topic I know I'm in the company of a large number of others.)
>
>
>

-- 



Reply via email to