cgi type=file problems

Maan Hamze mmhamze at pleiades.net
Mon Sep 3 12:57:12 EDT 2001


Dag
If I understand you correctly, you need to be able to browse for a file in a
form that contains:

Case 1:  using enctype=multipart/form-data
<input="file" name=userfile" size=40 "enctype=multipart/form-data">    #put
in your own values

Then you want to browse and be able to get the file name that you pick from
the browse button.
If you submit this form using:

import cgi
form cgi.FieldStorage()
print form["userfile"].value

What you get is not the file name but the actual file (binary or text).  To
get the actual string in the field use:

import cgi
form cgi.FieldStorage()
print form["userfile"].filename

This prints the actual string in the field.

Case 2:  if you do not use enctype=multipart/form-data
THEN

print form["userfile"].value
does give you the actual string used through the browse button.
And
print form["userfile"].filename

prints None

does this cover what you want?
Maan

"Dag" <dag at animagicnet.no> wrote in message
news:mailman.999512165.15540.python-list at python.org...
>
>
> > On 3 Sep 2001, Dag wrote:
> > D> I have a HTML form with a type=file input element and I simply want
to
> > D> get the file name as a string.  I'm not interested in the actual
file.
> > D> However the value from the file part of the form doesn't seem to get
> > D> passed at all.
> > D> For example I have the following form and script;
> > D>
> > D> <form action="/cgi-bin/cgi-test.py" method="post">
> >
> > <form action="/cgi-bin/cgi-test.py" method="post"
> enctype="multipart/form-data">
>
> Thanks, but unfortunately this doesn't quite seem to work the way I want
it
> to.  adding enctype="multipart/form-data" seems to makes the script upload
> the entire file, which is something I want to avoid, since I only need the
> filename and just want to use the file browsing to simplify entering the
> data, and make sure no typos are made.  Copying several megs across the
> network to simply extract a 50 byte string seems a bit of a waste.
>
> Dag
>
>





More information about the Python-list mailing list