我在使用cherrypy3.2.4上传文件时遇到问题。(Python 2.7)
我无法获取上传文件的原始数据。我试图调试如何从响应中获取数据值,但没有成功。有人知道怎么解决这个问题吗?在
皮奥特
这是我使用的代码:def index(self):
return """
Upload a file
filename:
Download a file
"""
index.exposed = True
def upload(self, myFile):
out = """
myFile length: %s
myFile filename: %s
myFile mime-type: %s
"""
cherrypy.log('-'*60)
cherrypy.log('0: ' + str(dir(myFile)))
cherrypy.log('-'*60)
cherrypy.log('2: ' + str(dir(cherrypy.request.headers)))
cherrypy.log('-'*60)
cherrypy.log('4: ' + str(dir(cherrypy.request.rfile)))
cherrypy.log('-'*60)
cherrypy.log('6: ' + str(cherrypy.request.rfile.readline))
cherrypy.log('-'*60)
cherrypy.log('8: ' + str(cherrypy.request.rfile.read()))
cherrypy.log('-'*60)
cherrypy.log('-'*60)
cherrypy.log('-'*60)
# Although this just counts the file length, it demonstrates
# how to read large files in chunks instead of all at once.
# CherryPy reads the uploaded file into a temporary file;
# myFile.file.read reads from that.
size = 0
while True:
data = myFile.file.read(8192)
if not data:
break
size += len(data)
#cherrypy.log('myFile: %s' % str(dir(myFile)))
#cherrypy.log('File: %s' % str(myFile.read_into_file))
#myFile.read_into_file('a.txt')
return out % (size, myFile.filename, myFile.content_type)
upload.exposed = True
这是输出:
^{pr2}$
编辑:
我试着找到了一个简单的解决方案:def upload(self, myFile):
lcHDRS = {}
for key, val in cherrypy.request.headers.iteritems():
lcHDRS[key.lower()] = val
incomingBytes = int(lcHDRS['content-length'])
content = myFile.file.read(incomingBytes)
file = open ('./upload/' + file.filename,"wb")
file.write(content)
file.close()
return 'File was uploaded...'