I have code that is to upload an image file from the user's system, but on the server side, the uploaded image is not displayed and when I try to open manually, it says file format not supported Here is the html code
<html>
<head>
<title>DICON Employee Form</title>
</head>
<body>
<form enctype = "multipart/form-data" method = "post" action = "http://localhost/cgi-bin/DICON_FORM.py">
<p>File: <input type = "file" name = "filename" /></p>
<input type = "submit" value = "Upload" />
</form>
</body>
</html>
Here is the cgi-python script
#!c:\Python27\python.exe
import cgi
import cgitb
import os
import MySQLdb
cgitb.enable()
form = cgi.FieldStorage()
if not form.has_key("EmployeeID"):
print "Location: /DICON_FORM.html\n"
else:
print """Content-type: text/html
<head><title>FORM</title></head>
<body>"""
employeeID = form["EmployeeID"].value
firstName = form["FirstName"].value
lastName = form["LastName"].value
others = form["Others"].value
fileitem = form["filename"]
fn = ""
if fileitem.filename:
fn = os.path.basename(fileitem.filename)
open(fn, 'wb').write(fileitem.file.read())
print "</body></html>"
The solution is to change the first line in the python script to #!c:\Python27\python.exe - u This opens the compiler in binary mode and images can then be written correctly