Skip to main content
 首页 » 编程设计

python之如何在 Odoo Python 中使文件可下载

2024年10月25日9yxwkf

我在我的模块中创建了一个生成静态 Excel 报告的向导,它运行良好。问题是有些客户远程访问我的模块,当他们打开向导并单击按钮时,生成的报告保存在我的本地驱动器上,但他们什么也得不到,是否有任何解决方案可以将我的 XLSX 报告制作为可下载文件和客户可以直接下载吗?

这是我试过但没有用的方法:

data = wizard.plugin_file 
file_path = wizard.file_path 
my_report_data = open('C:/Users/Report.xlsx','rb') 
f = my_report_data.read() 
output = base64.encodestring(f) 
self.write(cr, uid, ids[0], {'plugin_file': output}, context = context) 
view_obj = self.pool.get('ir.ui.view') 
view_id = view_obj.search(cr, uid, [('model', '=', 'report.model'), \ 
                            ('name', '=', 'report_model_form_view2')]) 
 
return { 
    'name': 'Monthly Report ', 
    'view_type': 'form', 
    'view_mode': 'form', 
    'res_model': 'report.model', 
    'view_id': view_id, 'form' 
    'res_id': ids[0], 
    'type': 'ir.actions.act_window', 
    'target': 'new', 
    'context': context, 
} 

表格:

<field name="file_path" invisible="1"/> 
<field name="plugin_file" filename="file_path"/> 

请您参考如下方法:

以下代码在导出 csv 文件时运行良好。

import cStringIO 
import base64 
def your_export_function(self): 
    fd = cStringIO.StringIO() 
    #use this file descriptor instead of file path 
    #after loading everydata to your file discriptor 
    out = base64.encodestring(fd.getvalue()) 
    fd.close()         
    self.write(cr, uid, ids, { 'data':out}, context=context) #data is the binary field in openerp