【问题标题】:Image file uploaded to server is corrupted/format not supported; python, cgi and html上传到服务器的图像文件损坏/格式不支持;蟒蛇,cgi和html
【发布时间】:2019-06-17 14:09:20
【问题描述】:

我有从用户系统上传图像文件的代码,但在服务器端,上传的图像不显示,当我尝试手动打开时,它说文件格式不支持 这是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-2.7 cgi server-side-scripting


    【解决方案1】:

    解决方法是将python脚本中的第一行改为 #!c:\Python27\python.exe - 你 这将以二进制模式打开编译器,然后可以正确写入图像

    【讨论】:

      猜你喜欢
      • 2012-08-03
      • 1970-01-01
      • 2016-01-06
      • 2012-06-08
      • 1970-01-01
      • 1970-01-01
      • 2014-04-26
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多