Not elegant, but finally works :
rows = db().select(db.mytable.id, db.mytable.upload_field,
db.mytable.upload_field_blob)
for i, row in enumerate(rows):
if row.upload_field != '' and row.upload_field is not None:
file = cStringIO.StringIO()
file.write(row.upload_field_blob)
db(db.mytable.id == row.id).update(
upload_field_upload=db.mytable.upload_field_upload.store(
file=file,
filename=db.mytable.upload_field.retrieve(row.upload_field)[0],
path=None))
db.commit()
filename =
db.mytable.upload_field_upload.retrieve(db.mytable(row.id).upload_field_upload,
nameonly=True)[1].split('/')[-1]
separate_folder_name =
db.mytable.upload_field_upload.retrieve(db.mytable(row.id
).upload_field_upload,
nameonly=True)[1].split('/')[-1].split('.')[2][:2]
path_tuple = (request.folder, 'uploads',
str(db.mytable.upload_field_upload), separate_folder_name)
path = os.path.join(*path_tuple)
if not os.path.exists(path):
os.makedirs(path)
f = open(os.path.join(path, filename), 'w')
f.write(row.upload_field_blob)
f.close()
Can't figure it out why file don't get wrote by .store() method... I mean
files get create with 0K, and I have to overwrite them with the code below
the db.commit()
:(
At least it works, if I can find why file don't get wrote in the first
place I may consider making a slice, but now it is to hacky... I get the
work done at least...
:)
Richard
On Sun, May 4, 2014 at 3:56 PM, Richard Vézina
<[email protected]>wrote:
> In [16]: db.mytable.fieldname_field.retrieve(row.fieldname_field,
> nameonly=True)[1]
> Out[16]: <cStringIO.StringI at 0x48cf9f0>
>
> In [17]: db.mytable.fieldname_field.retrieve(row.fieldname_field)[1]
> Out[17]: <cStringIO.StringI at 0x48cf9f0>
>
> Idem :(
>
> Richard
>
>
> On Sun, May 4, 2014 at 3:49 PM, Richard Vézina <
> [email protected]> wrote:
>
>> I saw the StringIO content of the blob. I had write a file manually with
>> that blob content... I may have not look carefully... I will have another
>> look.
>>
>> Richard
>>
>>
>> On Sun, May 4, 2014 at 2:51 PM, Niphlod <[email protected]> wrote:
>>
>>> not sure what you want to accomplish.... but....
>>>
>>>
>>> http://web2py.readthedocs.org/en/latest/dal.html#gluon.dal.DAL.Field.retrieve
>>>
>>> you're using nameonly, that explicitely doesn't return any content.
>>>
>>>
>>> On Sunday, May 4, 2014 7:58:03 PM UTC+2, Richard wrote:
>>>>
>>>> Hello,
>>>>
>>>> I try to extract file from upload field of type blob to store my files
>>>> on file system instead.
>>>>
>>>> I get there so far :
>>>>
>>>> rows = db().select(db.mytable.id, db.mytable.filename_field,
>>>> db.mytable.blob_field)
>>>> for i, row in enumerate(rows):
>>>> if row.filename_field != '' and row.filename_field is not None:
>>>> db(db.mytable.id == row.id).update(
>>>> new_field_upload=db.mytable.filename_field.store(
>>>> db.mytable.filename_field.retrieve(row.filename_field,
>>>> nameonly=True)[1], # Blob content, the file
>>>> db.mytable.filename_field.retrieve(row.filename_field,
>>>> nameonly=True)[0], # Filename
>>>> None
>>>> ))
>>>> if i % 10 == 0:
>>>> db.commit()
>>>>
>>>> db.commit()
>>>>
>>>>
>>>> The filename get created and sotered correctly in new_field_upload,
>>>> but the file from the blob content don't get write in
>>>> uploads/mytable.new_field_upload/HERE_IN_SUB_RANDOM_FOLDER_NAMES
>>>>
>>>> Notting get write to disk... Is .store() method not suppose to doing
>>>> just that?
>>>>
>>>> I will make a slice about that if I can get it to work properly.
>>>>
>>>> I was despointed this snippet wasn't there already.
>>>>
>>>> :(
>>>>
>>>> Thanks
>>>>
>>>> Richard
>>>>
>>> --
>>> Resources:
>>> - http://web2py.com
>>> - http://web2py.com/book (Documentation)
>>> - http://github.com/web2py/web2py (Source code)
>>> - https://code.google.com/p/web2py/issues/list (Report Issues)
>>> ---
>>> You received this message because you are subscribed to the Google
>>> Groups "web2py-users" group.
>>> To unsubscribe from this group and stop receiving emails from it, send
>>> an email to [email protected].
>>> For more options, visit https://groups.google.com/d/optout.
>>>
>>
>>
>
--
Resources:
- http://web2py.com
- http://web2py.com/book (Documentation)
- http://github.com/web2py/web2py (Source code)
- https://code.google.com/p/web2py/issues/list (Report Issues)
---
You received this message because you are subscribed to the Google Groups
"web2py-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
For more options, visit https://groups.google.com/d/optout.