将 deform.FileData 架构节点另存为文件

Save deform.FileData schema node as file

我的脚本与 Deform File Upload Widget example:

@view_config(renderer='templates/form.pt', name='file')
@demonstrate('File Upload Widget')
def file(self):

    class Schema(colander.Schema):
        upload = colander.SchemaNode(
            deform.FileData(),
            widget=deform.widget.FileUploadWidget(tmpstore)
            )

    schema = Schema()
    form = deform.Form(schema, buttons=('submit',))

    return self.render_form(form, success=tmpstore.clear)

使用 test_file.grf 捕获的上传是一个 deform.FileData schema node,看起来像:

>> captured['upload']
{'filename': u'test_file.grf',
 'fp': <tempfile._TemporaryFileWrapper object at 0x000000000638A6A0>,
  'mimetype': 'text/plain',
  'preview_url': None,
  'size': -1,
  'uid': '42DXY7DYW3'}

问题

如何将 deform.FileData 保存为特定位置的文件?

尝试打开文件并将其复制到位置 src 给出 TypeError:

with open(captured['upload']['fp'], 'r') as f:
    shutil.copyfileobj(f, src)

通过二进制打开文件简单地解决了它:

with open(src, 'wb') as f:
    f.write(captured['upload']['fp'].read())