[英]cgi python script seems not to receive uploaded file from html form
I'm trying to set up a file upload web form that is processed by a python script. 我正在尝试设置由python脚本处理的文件上载Web表单。 When I select a file and click upload, it says that no file was uploaded. 当我选择一个文件并单击上传时,它表示没有上传文件。 The file
field of the fileitem
object is None. fileitem
对象的file
字段为None。 This script is running on a lighthttpd server. 此脚本在lighthttpd服务器上运行。
The code for the script is here: 脚本的代码在这里:
#!/usr/bin/env python
import cgi, os
import cgitb
cgitb.enable()
form = cgi.FieldStorage()
# A nested FieldStorage instance holds the file
fileitem = form['filename']
print "----"
print "filename", fileitem.filename
print "file", fileitem.file
print "----"
message = ''
if fileitem.file:
# It's an uploaded file; count lines
linecount = 0
while 1:
line = fileitem.file.readline()
if not line: break
linecount = linecount + 1
message = linecount
# Test if the file was uploaded
if fileitem.filename:
# strip leading path from file name to avoid directory traversal attacks
fn = os.path.basename(fileitem.filename)
open('/var/cache/lighttpd/uploads/' + fn, 'wb').write(fileitem.file.read())
message = 'The file "' + fn + '" was uploaded successfully'
else:
message += 'No file was uploaded'
print """\
Content-Type: text/html\n
<html><body>
<p>%s</p>
</body></html>
""" % (message,)
The html file is here: html文件在这里:
<html>
<head>
<title>RepRap Pinter</title>
</head>
<body>
<H1>RepRap Printer</H1>
<form action="cgi-bin/start_print.py" method="post" encrypt="multipart/form-data">
<p><input type="file" name="filename" id="file"></p>
<p><input type="submit" value="Upload"></p>
</form>
</body>
</html>
And the output is: 输出是:
----
filename None
file None
----
Content-Type: text/html
<html><body>
<p>No file was uploaded</p>
</body></html>
Any ideas as to why the file isn't being uploaded? 有关为什么文件没有上传的任何想法?
Your problem seems to be here: 你的问题似乎在这里:
<form ... encrypt="multipart/form-data">
The attribute you are looking for isn't encrypt
, it's enctype
. 您正在寻找的属性不是encrypt
,它是enctype
。 Since you are missing the correct parameter your encoding isn't multipart-formdata so file uploads are ignored. 由于缺少正确的参数,因此编码不是multipart-formdata,因此会忽略文件上载。
You are using the wrong attribute name: 您使用了错误的属性名称:
<form action="cgi-bin/start_print.py" method="post" encrypt="multipart/form-data">
The correct attribute is enctype
, not encrypt
: 正确的属性是enctype
,而不是encrypt
:
<form action="cgi-bin/start_print.py" method="post" enctype="multipart/form-data">
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.