gbs 0 Newbie Poster

I was trying to write a simple python script to enable web users to upload files.

Here is html

 <FORM ACTION="" METHOD="POST" enctype="multipart/form-data">
<INPUT TYPE="text" NAME="words" SIZE=30><br>
<INPUT TYPE="file" NAME="infile" SIZE=30><br>
 <INPUT TYPE="submit" NAME="Submit" VALUE="Submit Form">

Here is the python script

import cgi, sys
def error(message):
        print """<html><head><title>Error</title></head><body>
        <p> Error Message: </p><p> You have to %s!</p>
        </body></html>""" % (message)
print "Content-Type: text/html\n\n"
form = cgi.FieldStorage()
if not form.has_key("words"):
    error("enter sth")
if not form.has_key("infile"):
    error("specify a local file")
print """<html><head><title>Thank you</title></head>
<p> Thank you for your submission! </p>

Here is the problem:
I have to specify the value of enctype to be "multipart/form-data", since there is an input element of type "file".

However, in this case, when a user clicks submit without filling any of those two inputs, the "Thank you" page rather than the "Error" page still returns to the user's browser.

ONLY after the enctype is changed to be its default value: "application/x-www-form-urlencoded", the "Error Message" page is rendered.

Anyone knows why?


Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.18 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.