corrupted/format 不支持上传到服务器的图片文件; python、cgi 和 html
Image file uploaded to server is corrupted/format not supported; python, cgi and html
我有从用户系统上传图片文件的代码,但是在服务器端,上传的图片没有显示,当我尝试手动打开时,它说不支持文件格式
这是 html 代码
<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>
这是cgi-python脚本
#!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>"
解决方法是将python脚本中的第一行改成
#!c:\Python27\python.exe - u
这将以二进制模式打开编译器,然后可以正确写入图像
我有从用户系统上传图片文件的代码,但是在服务器端,上传的图片没有显示,当我尝试手动打开时,它说不支持文件格式 这是 html 代码
<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>
这是cgi-python脚本
#!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>"
解决方法是将python脚本中的第一行改成 #!c:\Python27\python.exe - u 这将以二进制模式打开编译器,然后可以正确写入图像