473,241 Members | 1,488 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,241 software developers and data experts.

Why is "if fileitem.file:" (almost) never false?

How do I detect that a particular form element is a file upload or if
the file upload has worked?

In the Python cgi module documentation I found suggested code...

form = cgi.FieldStorage()
fileitem = form["userfile"]
if fileitem.file:
# It's an uploaded file; count lines
I've tried something like this in a test script and fileitem.file
evaluates to true when form element "userfile" is just a text box in
the web page. When "userfile" is a type="file" it evaluates true when I
select a real file to upload and when I manually type in a false path
to a file that doesn't exist. When I do upload a file, the script
correctly prints out the file contents (fileitem.value) so the upload
does work OK.

It seems that provided there's a form element called "userfile" then
fileitem.file evaluates to true provided the form has
enctype="multipart/form-data" (it doesn't matter if the file upload
works or if the form element is not type="file").

When I tried different forms without the enctype then fileitem.file
evaluated to false when userfile was either type="text" or type="file".

My conclusion is that if fileitem.file: detects enctype of form rather
than if a particular element is a file upload. How do I detect that a
particular form element is a file upload or if the file upload has
worked?

My best suggestion (but I'd like to hear your views) is to use:
if fileitem.filename and len(fileitem.value):
# It's an uploaded file

When the form element userfile is just a text box or the form lacks the
enctype then the filename attribute evaluates to false. When I supply
an incorrect file path to the file form element the upload fails so
len(fileitem.value) evaluates to false. When I upload a real file, both
fileitem.filename and len(fileitem.value) evaluate to true.

If I upload a real file that's empty then fileitem.value is an empty
string so len(fileitem.value) evaluates to false which is not quite
right (an empty file is an obscure test but it is a valid file to
upload).

Any suggestions for improvements would be gratefully received.

Thanks

Mark

PS if it's relevant, I'm using Windows2000, Python 2.3.2, the shebang
includes -u, Apache 2.0.42 and Firefox 1.0.4.

Nov 22 '05 #1
2 3379
I think:

form = cgi.FieldStorage()
fileitem = form["userfile"]
if fileitem.filename:
data = form.getfirst("userfile")
if data:
#do somethinfg with it
FieldStorage objects always have a filename attribute which is the
filename that the user used to upload the file or '' (maybe None) when
the FieldStorage object was not created for a File upload. The
getfirst call returns the data of the file, but you need to access the
FieldStorage object directly forthe filename (as you did).

HTH

Peter
mark wrote:
How do I detect that a particular form element is a file upload or if
the file upload has worked?

In the Python cgi module documentation I found suggested code...

form = cgi.FieldStorage()
fileitem = form["userfile"]
if fileitem.file:
# It's an uploaded file; count lines
I've tried something like this in a test script and fileitem.file
evaluates to true when form element "userfile" is just a text b ox in the web page. When "userfile" is a type="file" it evaluates true when I
select a real file to upload and when I manually type in a false path
to a file that doesn't exist. When I do upload a file, the script
correctly prints out the file contents (fileitem.value) so the upload
does work OK.

It seems that provided there's a form element called "userfile" then
fileitem.file evaluates to true provided the form has
enctype="multipart/form-data" (it doesn't matter if the file upload
works or if the form element is not type="file").

When I tried different forms without the enctype then fileitem.file
evaluated to false when userfile was either type="text" or type="file".

My conclusion is that if fileitem.file: detects enctype of form rather
than if a particular element is a file upload. How do I detect that a
particular form element is a file upload or if the file upload has
worked?

My best suggestion (but I'd like to hear your views) is to use:
if fileitem.filename and len(fileitem.value):
# It's an uploaded file

When the form element userfile is just a text box or the form lacks the
enctype then the filename attribute evaluates to false. When I supply
an incorrect file path to the file form element the upload fails so
len(fileitem.value) evaluates to false. When I upload a real file, both
fileitem.filename and len(fileitem.value) evaluate to true.

If I upload a real file that's empty then fileitem.value is an empty
string so len(fileitem.value) evaluates to false which is not quite
right (an empty file is an obscure test but it is a valid file to
upload).

Any suggestions for improvements would be gratefully received.

Thanks

Mark

PS if it's relevant, I'm using Windows2000, Python 2.3.2, the shebang
includes -u, Apache 2.0.42 and Firefox 1.0.4.


Nov 22 '05 #2
I think:

form = cgi.FieldStorage()
fileitem = form["userfile"]
if fileitem.filename:
data = form.getfirst("userfile")
if data:
#do somethinfg with it
FieldStorage objects always have a filename attribute which is the
filename that the user used to upload the file or '' (maybe None) when
the FieldStorage object was not created for a File upload. The
getfirst call returns the data of the file, but you need to access the
FieldStorage object directly forthe filename (as you did).

HTH

Peter
mark wrote:
How do I detect that a particular form element is a file upload or if
the file upload has worked?

In the Python cgi module documentation I found suggested code...

form = cgi.FieldStorage()
fileitem = form["userfile"]
if fileitem.file:
# It's an uploaded file; count lines
I've tried something like this in a test script and fileitem.file
evaluates to true when form element "userfile" is just a text b ox in the web page. When "userfile" is a type="file" it evaluates true when I
select a real file to upload and when I manually type in a false path
to a file that doesn't exist. When I do upload a file, the script
correctly prints out the file contents (fileitem.value) so the upload
does work OK.

It seems that provided there's a form element called "userfile" then
fileitem.file evaluates to true provided the form has
enctype="multipart/form-data" (it doesn't matter if the file upload
works or if the form element is not type="file").

When I tried different forms without the enctype then fileitem.file
evaluated to false when userfile was either type="text" or type="file".

My conclusion is that if fileitem.file: detects enctype of form rather
than if a particular element is a file upload. How do I detect that a
particular form element is a file upload or if the file upload has
worked?

My best suggestion (but I'd like to hear your views) is to use:
if fileitem.filename and len(fileitem.value):
# It's an uploaded file

When the form element userfile is just a text box or the form lacks the
enctype then the filename attribute evaluates to false. When I supply
an incorrect file path to the file form element the upload fails so
len(fileitem.value) evaluates to false. When I upload a real file, both
fileitem.filename and len(fileitem.value) evaluate to true.

If I upload a real file that's empty then fileitem.value is an empty
string so len(fileitem.value) evaluates to false which is not quite
right (an empty file is an obscure test but it is a valid file to
upload).

Any suggestions for improvements would be gratefully received.

Thanks

Mark

PS if it's relevant, I'm using Windows2000, Python 2.3.2, the shebang
includes -u, Apache 2.0.42 and Firefox 1.0.4.


Nov 22 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Jason Charalambides | last post by:
I set a program to automatically load values from a temporary file. However, there is a chance that the specific temporary file "C:\Temp\TU.tmp" may not exist at all. In that case I want that...
10
by: george young | last post by:
I had developed the habit of using the neat python form: if someinstance: someinstance.memb() because it seems cleaner than "if someinstance is not None". {please no flames about "is not None"...
10
by: mike | last post by:
regards: I use Jtidy (api) to translate a HTML file into a "XHTML file". But The "XHTML file" cannot be identified by nokia 6600. Do I miss something important? Or this is Jtidy's weakness or...
5
by: kmunderwood | last post by:
I am trying to combine "if match=" and "when test" I am a newbie, and have made both work separately, but I can not seem to combine them. This is my xml("index.xml")page(I can not change this,...
145
by: Sidney Cadot | last post by:
Hi all, In a discussion with Tak-Shing Chan the question came up whether the as-if rule can cover I/O functions. Basically, he maintains it can, and I think it doesn't. Consider two...
0
by: mark | last post by:
How do I detect that a particular form element is a file upload or if the file upload has worked? In the Python cgi module documentation I found suggested code... form = cgi.FieldStorage()...
3
by: Chris | last post by:
Hi, 1) In file test.aspx, i put: <%@ Page Language="VB" AutoEventWireup="false" CodeFile="test.aspx.vb" Inherits="test" %> <%@ import namespace="System.Data"%> <%@ import...
37
by: jht5945 | last post by:
For example I wrote a function: function Func() { // do something } we can call it like: var obj = new Func(); // call it as a constructor or var result = Func(); // call it as...
2
by: John Nagle | last post by:
For some reason, Python's parser for "robots.txt" files doesn't like Wikipedia's "robots.txt" file: False The Wikipedia robots.txt file passes robots.txt validation, and it doesn't disallow...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.