My script is exactly as the 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)
the captured upload with test_file.grf
is a deform.FileData
schema node which looks like:
>> captured['upload']
{'filename': u'test_file.grf',
'fp': <tempfile._TemporaryFileWrapper object at 0x000000000638A6A0>,
'mimetype': 'text/plain',
'preview_url': None,
'size': -1,
'uid': '42DXY7DYW3'}
Question
How to save deform.FileData
as a file on a specific location?
An attempt is to open and copy the file to location src
gave a TypeError
:
with open(captured['upload']['fp'], 'r') as f:
shutil.copyfileobj(f, src)
Solved it simply by binary opening the file:
with open(src, 'wb') as f:
f.write(captured['upload']['fp'].read())