python-2.7 - 在 ZPublisher.HTTPRequest.FileUpload 上使用 is_zipfile

标签 python-2.7 plone zope

我正在尝试检查上传的文件是否是有效的 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/

相关文章:

python - Zope接口(interface)是如何实现的?

c - 如何在 Python 列表中动态插入数据?

database - 皮威 : How to update specific fields?

python - 将 python 模块添加到 Azure

python - Numpy 随机数生成在矢量化后运行速度变慢

python - 为 Plone 产品翻译文件系统中的内容

plone - 如何为 z3cform 创建一个新的 contenttreewidget

plone - ZODB 内存后端?

mysql - Plone + SqlAlchemy + MySql 中未提交的事务

performance - 提高 Plone 搜索结果中大型表的性能