【发布时间】:2012-01-29 09:20:13
【问题描述】:
我设置了linux+apache2.2.+wsgi+python环境来测试文件上传。 共有2个页面,一是让用户选择要上传的文件;另一个是处理文件上传。
预期结果:
上传的文件内容正确。
实际结果:
上传了一个文件,但内容是原始文件内容加上 http 标头和开始/结束行的一部分。喜欢:
-----------------------------40976349392994148594600211
Content-Disposition: form-data; name="filename"; filename="configure.scan"
Content-Type: application/octet-stream
[original file content]
-----------------------------40976349392994148594600211
有人可以给我答案吗?我将深深感谢您的帮助。
首页代码:
output= '<html><head>' +\
'<br>' + \
'</head><body>' + \
'<form name="form1" action=“/dynamic/postuploadfile.py” enctype="multipart/form-data" method=“post”>' +\
'File: <input type="file" name="test" size=50><br />' +\
'<input type=“submit” value="upload"/>' +\
'</form></body></html>'
def application(environ, start_response):
status = '200 OK'
response_headers = [('Content-type', 'text/html'),
('Content-Length', str(len(output)))]
start_response(status, response_headers)
return [output]
第二页代码:
import os
def upload(environ):
# A nested FieldStorage instance holds the file
#fileitem = req.form['file']
data = environ['wsgi.input'].read(int(environ.get('CONTENT_LENGTH','0')))
message = ''
open('uploaded', 'wb').write(data)
message = 'The file was uploaded successfully'
return ( '<html><body>' + message + '</body><html>' )
def application(environ, start_response):
status = '200 OK'
output = upload( environ )
response_headers = [('Content-type', 'text/html'),
('Content-Length', str(len(output)))]
start_response(status, response_headers)
return [output]
【问题讨论】: