我的脚本与变形文件上传小部件示例完全相同:
@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
模式节点,如下所示:
>> 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)