Help

欢迎!

该社区面向专业人士和我们产品和服务的爱好者。
分享和讨论最好的内容和新的营销理念,建立您的专业形象,一起成为更好的营销人员。


0

How to fix base64.encodestring issue?

Avatar
odoo
Avatar
Discard
1 Answer
0
Avatar
odoo
Best 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,
                            })

13 Comments
Avatar
Discard
Avatar
odoo
-

Thank you sir for the response, i tried your suggestion but the terminal says: AttributeError: '_csv.writer' object has no attribute 'close'

Avatar
odoo
-

Is it ok for StringIO ? For the csv.writer just try without close.

Avatar
odoo
-

I have tried in my other method (file_writer method) and it rendered the view, however no object on fields file_data. In my csv_writer method it produce an AttributeError.

Avatar
odoo
-

which AttributeErro ? can you put entire trace into your question ? It is easy to resolve your issue.

Avatar
odoo
-

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'

Avatar
odoo
-

I use virtual_file.close() on my csv_writer method but it produce an error like this one: virtual_file.close() AttributeError: '_csv.writer' object has no attribute 'close'

Avatar
odoo
-

Hi, I have update my answer. Please try it and let me know the result.

Avatar
odoo
-

Sir it produce another error, here is what it says: virtual_file.seek(0) AttributeError: '_csv.writer' object has no attribute 'seek'

Avatar
odoo
-

May I know that which library you have import in your module ?

Avatar
odoo
-

Here are some of my imports sir: import logging import base64 import csv

Avatar
odoo
-

Hi, I am confused because csv have all of those (close, seek, etc.) attributes but in your system it gives an attribute error. So, please try as I have given you in my updated answer.

Avatar
odoo
-

Have you tried updated answer and then you got an error while down load "Incorrect padding" ? ?

Avatar
odoo
-

I really don't know what is going on with the code, but every time i plug csv attributes it pops up AttributeError ('_csv.writer' object has no attribute 'close' and the like.)