我正在尝试检查上传的文件是否是有效的 zip 文件,但似乎 is_zipfile 调用了 read 方法,该方法将文件字符串设置为 '',因此后续调用 read 返回零长度字符串。
我正在尝试复制文件以获得第二个一次性版本,但复制只是浅复制,而深层复制返回错误TypeError: object.__new__(method-wrapper) is not safe, use method -wrapper.__new__()
我可以将文件字符串保存到变量中,然后调用 is_zipfile 方法,但这会返回 False,因为该文件现在实际上是一个空文件。
如何复制 FileUpload 对象,或调用 is_zipefile 而不调用 read 方法,或验证该对象是 zip 文件而不在此过程中破坏它?
fileToImport = REQUEST.get('sourceFile', None)
if is_zipfile(fileToImport):
file_string = fileToImport.read()
self.importDesignFromZip(file_string, replace=replace)
else:
xmlstring = fileToImport.read()
self.importDesignFromXML(xmlstring, replace=replace)
干杯 迈克尔
Plone -4.1.3 佐普 2.13.10 Python 2.7.3
最佳答案
返回到文件的开头:
iszip = is_zipfile(fileToImport)
fileToImport.seek(0)
data = fileToImport.read()
if iszip:
self.importDesignFromZip(data, replace=replace)
else:
self.importDesignFromXML(data, replace=replace)
关于python-2.7 - 在 ZPublisher.HTTPRequest.FileUpload 上使用 is_zipfile,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17616629/