1 Answer
Hi,
You just need to close your file which is attached with your writer object. And then you have to again open for read and pass it to the base64.encodestring() function. Please have a look on the below.
virtual_file.seek(0)
self.create(cr,uid,{
'name' : 'Studentlist',
'file_data' : base64.encodestring(virtual_file.read()),
})
vrtual_file.close()
Note : You have passed writer object to the base64.encodestring() function thats why you face an error.
Please try according my solution as like below.
file_point = 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):
file_point.write(str(obj_list.last_name)+","+str(obj_list.first_name)+","+str(obj_list.middle_name))
file_point.close()
file_point = open('/tmp/student.csv','rb')
file_data = base64.encodestring(file_point.read())
self.create(cr,uid,{
'name' : 'Studentlist',
'file_data' : file_data,
})
This is what it produce sir: Traceback (most recent call last): File "/opt/openerp/server-7/openerp/netsvc.py", line 296, in dispatch_rpc result = ExportService.getService(service_name).dispatch(method, params) File "/opt/openerp/server-7/openerp/service/web_services.py", line 626, in dispatch res = fn(db, uid, *params) File "/opt/openerp/server-7/openerp/osv/osv.py", line 190, in execute_kw return self.execute(db, uid, obj, method, *args, **kw or {}) File "/opt/openerp/server-7/openerp/osv/osv.py", line 132, in wrapper return f(self, dbname, *args, **kwargs) File "/opt/openerp/server-7/openerp/osv/osv.py", line 199, in execute res = self.execute_cr(cr, uid, obj, method, *args, **kw) File "/opt/openerp/server-7/openerp/osv/osv.py", line 187, in execute_cr return getattr(object, method)(cr, uid, *args, **kw) File "/opt/openerp/biz1_server70_addons/biz1_bir_report/biz1_alphalist.py", line 131, in load_alphalist return self.pool.get('alphalist.minimum.prev.employer').load_all_employees(cr,uid,ids,data,context=context) File "/opt/openerp/biz1_server70_addons/biz1_bir_report/biz1_alphalist_forms.py", line 155, in load_all_employees id2 = self.call_file_writer(cr, uid, ids, context=context) File "/opt/openerp/biz1_server70_addons/biz1_bir_report/biz1_alphalist_forms.py", line 190, in call_file_writer return self.pool.get('file.holder').csv_writer(cr,uid,ids,context=context) File "/opt/openerp/biz1_server70_addons/biz1_bir_report/biz1_alphalist_forms.py", line 1351, in csv_writer virtual_file.close() AttributeError: '_csv.writer' object has no attribute 'close'