Hi everyone. Can anyone help me, pls!
I have my problem with base64 but i cant resolve it. What i'm doing is that i am trying to persist a file (either .DAT or .csv) on my database but upon running:
1. On my method file_writer, it says:
'file_data' : base64.encodestring(virtual_file),
File "/usr/lib/python2.7/base64.py", line 313, in encodestring for i in range(0, len(s), MAXBINSIZE):
AttributeError: StringIO instance has no attribute '__len__'
2. On my method csv_writer, it says:
'name' : 'Studentlist',
File "/usr/lib/python2.7/base64.py", line 313, in encodestring
for i in range(0, len(s), MAXBINSIZE):
TypeError: object of type '_csv.writer' has no len()
Here are my methods:
1.
def file_writer(self, cr, uid, ids, context=None):
virtual_file = StringIO()
student_object = self.pool.get('student.student')
active_id = student_object.search(cr,uid,[])
for obj_list in student_object.browse(cr,uid,active_id,context=context):
virtual_file.write(str(obj_list.last_name)+","+str(obj_list.first_name)+","+str(obj_list.middle_name))
self.create(cr,uid,{
'name' : 'Studentlist',
'file_data' : base64.encodestring(virtual_file),
})
return True
( Here, without base64 it persist into database but upon opening of downloaded file it shows something like this :
JÚâJÚâ{-jwjÝ1í÷5÷Î;kO| )
2.
def csv_writer(self, cr, uid, ids, context=None):
virtual_file = csv.writer(open("/tmp/Student.csv","wb"))
student_object = self.pool.get('student.student')
active_id = student_object.search(cr,uid,[])
for obj_list in student_object.browse(cr,uid,active_id,context=context):
virtual_file.writerow([str(obj_list.last_name),str(obj_list.first_name),str(obj_list.middle_name)])
self.create(cr,uid,{
'name' : 'Studentlist',
'file_data' : base64.encodestring(virtual_file),
})
return True
(Here, w/o base64 it shows:
File "/usr/lib/python2.7/base64.py", line 76, in b64decode
raise TypeError(msg)
TypeError: Incorrect padding
)
Any help is much appreciated.
Error image: