我借用了这段代码来将文件流保存到磁盘,除了文件大小小于 1kb 时,它都可以工作。我收到此错误:
in stuff_uploaded:
copy(theFile.file.name, './tmp/'+theFile.filename) #saves temporary file to /cp2/tmp/
AttributeError:“cStringIO.StringO”对象没有属性“名称”
@cherrypy.expose
@cherrypy.tools.noBodyProcess()
def stuff_uploaded(self, theFile=None):
import cgi
import tempfile
# convert the header keys to lower case
lcHDRS = {key.lower():val for key, val in cherrypy.request.headers.iteritems()}
class myFieldStorage(cgi.FieldStorage):
"""uses a named temporary file instead of the default non-named file; keeping it visibile (named), allows us to create a
2nd link after the upload is done, thus avoiding the overhead of making a copy to the destination filename."""
def make_file(self, binary=None):
return tempfile.NamedTemporaryFile()
formFields = myFieldStorage(fp=cherrypy.request.rfile,
headers=lcHDRS,
environ={'REQUEST_METHOD':'POST'},
keep_blank_values=True)
theFile = formFields['theFile']
# we now create a 2nd link to the file, using the submitted filename.
from shutil import copy
copy(theFile.file.name, './tmp/'+theFile.filename) #saves temporary file
msgs = csv_to_survey.match_fieldnames('./tmp/'+theFile.filename)
return './tmp/'+theFile.filename
那么我该怎么做才能确保 cStringIO.StringO 处理上传的小文件呢?