如何在Odoo 10中提供下载二进制文件的途径

时间:2019-05-22 22:12:01

标签: real-time odoo gtfs

我正在尝试在odoo 10中创建一条路由,以下载用于gtfs实时更新程序的二进制文件。

我在odoo文档中进行了搜索,但没有找到类似的

这是我用于测试下载二进制文件的控制器:

gif <- df %>% 
  mutate(group=factor(group), category=factor(category)) %>%
  ggplot() +
  geom_rect(aes(xmin=37.00, xmax=37.21, ymin=-Inf, ymax=Inf, fill=category), alpha=0.5, show.legend=T) + 
  geom_jitter(aes(x=lon, y=lat), size=5, width=0.02, height=0.02) +
  labs(x="Longitude", y="Latitude") +
  scale_x_continuous(limits=c(37.00, 37.21), expand=c(0,0)) +
  theme(panel.background=element_blank(), 
        axis.ticks.length=unit(0.25,"cm"),
        axis.ticks.x=element_line(size=0.2),
        axis.ticks.y=element_line(size=0.2),
        axis.line.x=element_line(size=0.2),
        axis.line.y=element_line(size=0.2),
        plot.margin=margin(t=5.5, r=5.5, b=5.5, l=5.5)) +
  transition_time(time) +
  view_follow(fixed_x=T, fixed_y=T) +
  labs(title = '{frame_time}') +
  ease_aes('linear')

animate(gif, height=500, width=500, duration=70, renderer=gifski_renderer()) # use duration to slow it down
anim_save("df.gif")

我想在/ gtfs_rt上下载文件

此刻,我收到此错误。

class GtfsRt(http.Controller):
    @http.route('/gtfs_rt', type='http', auth='public')
    def index(self):
        f = open('my_file', 'w+b')
        byte_arr = [120, 3, 255, 0, 100]
        binary_format = bytearray(byte_arr)
        f.write(binary_format)
        f.close()
        return f

谢谢您的帮助!

2 个答案:

答案 0 :(得分:1)

至少在Odoo 11中有效。如果任何模型中都有一个二进制字段:

@api.multi
def crete_and_download_dummy_field(self):

    # store here your binary file into the dummy field with base64 enconding

    return {
        'name': 'FEC',
        'type': 'ir.actions.act_url',
        'url': '/web/content/?model=sale.order&id={}&field=dummy&filename_field=dummy_filename&download=true'.format(
            self.id
        ),
        'target': 'self',
    }

如果只希望链接以某种形式下载,则可以创建一个二进制文件及其名称

mock_pdf = fields.Binary(string='Mock pdf')

mock_pdf_filename = fields.Char(
    string='Mock PDF Filename',
    compute='_compute_mock_pdf_filename'
)

@api.depends('mock_pdf')
def _compute_mock_pdf_filename(self):
    self.ensure_one()
    name = self.name.replace('/', '_')
    name = name.replace('.', '_')
    name = name + '.pdf'
    self.mock_pdf_filename = name

并以格式

<field name="mock_pdf_filename" invisible="1" />
<field name="mock_pdf" filename="mock_pdf_filename" />

答案 1 :(得分:0)

以下代码将在特定路径下下载xml文件。希望这对您有所帮助。

    xml = # xml contents.
    response = request.make_response(xml,
                                     headers=[('Content-Type', 'application/xml'),
                                              ('Content-Disposition', 'attachment; 
                                                       filename=Xmlreport.xml;')],
                                     cookies={'fileToken': token})


    return response