473,406 Members | 2,220 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,406 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 3404
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: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.