[mod_python] How to find a filename

Graham Dumpleton grahamd at dscpl.com.au
Sat Jan 20 23:17:21 EST 2007


On 21/01/2007, at 2:51 PM, sandipm at talentica.com wrote:

> other way of doing this..dont use req.form at all...
>
> from mod_python import util
>
> frm = util.FieldStorage(req)
>  upfile = frm.getlist('UploadFile')[0] # name of field
>  filename = upfile.filename
>  filedata = upfile.value
>
>
> you can get all form fields using frm.getlist('fieldname')[0]
>
>
>
> Thanks
>
> Sandip
Your getting a bit confused.

Where req.form comes into it is that it is the prevailing convention  
that if anything
ever creates an instance of util.FieldStorage, that it store it as  
req.form. It should
only create the instance of util.FieldStorage though if req.form  
doesn't already exist.

Thus the code for a handler becomes:

   if not hasattr(req, 'form'):
     req.form = util.FieldStorage(req)

   upfile = req.form.getfirst('UploadFile')
   if upfile:
     ...

This is what mod_python.publisher and mod_python.psp do in more  
recent versions.

Peoples comments about req.form were referring to that under the  
belief that the
person was using one of the handlers that leaves the form it creates  
as req.form.
The person was however using a basic handler where it isn't created  
and so they
would have to create it themselves. Following best practice, they  
should when
they create the form, store it as req.form as the code I give shows.  
Using this
convention you have a better chance of things working when  
integrating different
handler mechanisms together as you will inherit the form created by  
another
handler instead of creating it again and loosing your POST data.

BTW, one is best off using getfirst() instead of either subscript  
operator or getlist().
By using getfirst() in preference to getlist(), you do not have to  
then access the
first element as only one will be returned or None if it doesn't  
exist. If there is no
field, getlist() returns an empty list and access the first entry in  
the list will raise
an exception. It is also better than using subscript operator as it  
will raise an
exception if field doesn't exist also and if it does exist can return  
either a single
field object or a list of fields if there were duplicated entries.

Graham

> On Sat Jan 20 11:01 , Jim Gallacher sent:
>
>
> export at hope.cz wrote:
> > But how can I get the name of an uploaded file?
> > I tried util.FieldStorage but I received the content of the file  
> but not the
> > name of the file.
>
> Jorey already gave you that answer.
>
> filename = req.form['Picture'].filename
>
> Jim
>
>
> > Did I make a mistake somewhere?
> > Lad.
> >
> >
> >
> >> If you are not using publisher or the psp handler you'll need to  
> create
> >> a FieldStorage instance explicitly.
> >>
> >> from mod_python import util
> >>
> >> def handler(req):
> >> req.form = util.FieldStorage(req)
> >> ... and so on ...
> >>
> >> Jim
> >>
> >> export at hope.cz wrote:
> >> > Thank you for your reply but I do not use publisher.
> >> > Is there any other way?
> >> > Thank you for help
> >> > Lad
> >> >
> >> >
> >> >> export at hope.cz wrote:
> >> >>> I have a form through which users upload a file.
> >> >>>
> >> >>>
> >> >>>
>
>
>
> >> >>>
> >> >>>
> >> >>>
> >> >>> Is it possible to find out the name of a file the user is  
> being uploaded?
> >> >> If you are using Publisher, it will be available as:
> >> >>
> >> >> req.form['Picture'].filename
> >> >>
> >> >
> >> >
> >> > _______________________________________________
> >> > Mod_python mailing list
> >> > Mod_python at modpython.org
> >> > http://mailman.modpython.org/mailman/listinfo/mod_python
> >> >
> >>
> >
> >
> >  
> ---------------------------------------------------------------------- 
> --
> >
> > _______________________________________________
> > Mod_python mailing list
> > Mod_python at modpython.org
> > http://mailman.modpython.org/mailman/listinfo/mod_python
>
> _______________________________________________
> Mod_python mailing list
> Mod_python at modpython.org
> http://mailman.modpython.org/mailman/listinfo/mod_python
>
> _______________________________________________
> Mod_python mailing list
> Mod_python at modpython.org
> http://mailman.modpython.org/mailman/listinfo/mod_python

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mm_cfg_has_not_been_edited_to_set_host_domains/pipermail/mod_python/attachments/20070121/5207a30b/attachment.html


More information about the Mod_python mailing list