473,386 Members | 1,820 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,386 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 3401
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: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
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...

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.