On Wednesday, November 2, 2011 6:20:55 AM UTC-4, lucas wrote:
>
> one additional question, the response.download function confuses me 
> still.  i get it that it is download to the client computer.  what i 
> don't get is its parameters. 
>
> response.download(request, db) 
>
> i get that it pulls the last argument from the request args to get the 
> filename.  but this db parameter makes no sense to me because the db 
> may have multiple tables and how does it know which table to pulll the 
> file from?


Yeah, I'm working on explaining that better in the book. Note, when the 
file is initially uploaded, the new name it is given starts with 
tablename.fieldname of the upload field, so it extracts the table and field 
name from the filename itself. It then uses that to find the relevant 
upload field in the DB, and the only thing it needs from that field is the 
upload folder where it can find the file.
 

>  also, what if you have two fields in your "files" table, 
> one for the name of the file and the other for the extension, like 
> "pdf", how will response.download know which record to access to 
> download the correct file?  so, how does response.download know which 
> table and which row to pull the file from the database (db) to 
> download it to the client?
>

Note, when you upload a file, you don't have to store the filename or 
extension separately in the table (though you can if you need to for some 
reason). During the upload process, web2py encodes the original filename 
into the new filename (which it then decodes upon download), and it 
preserves the extension as well.

So, when you upload a file called 'myfile.pdf' into upload field 'myfield' 
in 'mytable', the new filename will be:

mytable.myfield.[16-char-uuid].[base16-encoded 'myfile'].pdf

response.download extracts the table and field name, decodes the original 
filename, and identifies the upload folder associated with mytable.myupload 
(which is an attribute of the field itself -- it defaults to /app/uploads, 
but can be set differently via the field's 'uploadfolder' argument).

Anthony

Reply via email to