Q:

Odoo: Download binary field content as text file

i am working on an addons to export invoices in a semi-colon separated file. I have created a wizard to choose invoices one wants to export and i am able to write data in file located in the my_module/data/invoices.txt. Now i want to download this file, i used the saveas method in the web addon but i end up with a 404 error (i have a binary field (txt_file) in the wizard to store the file's content as base64 encoded data).
Here is the code i am using:
@api.multi
def export(self):
    self.ensure_one()
    if self.invoice_ids:
        lines = []
        for line in self.invoice_ids:
            str_line = ''
            str_line += str(line.partner_id.name) + ';'
            str_line += str(line.number) + ';'
            str_line += str(line.origin) + ';'
            str_line += str(line.date_invoice) + ';'
            str_line += str(line.date_due) + ';'
            str_line += line.account_id.name + ';'
            str_line += line.journal_id.name + ';'
            str_line += str(line.amount_untaxed) + ';'
            str_line += str(line.amount_total) + ';'
            str_line = str_line.encode('utf-8')
            lines.append(str_line)
        import os.path

        my_path = os.path.abspath(os.path.dirname(__file__))
        path = os.path.join(my_path, "../data/invoices.txt")

        file = open(path, 'w+')
        if file:
            output = ''
            for line in lines:
                output += line + '\n'
            self.txt_file = base64.b64encode(output)

            file.write(base64.b64decode(self.txt_file))
            wizard = self.env['export_invoice.wizard'].with_context({'active_id': self.id}).create({})
            # I have found this on the web
            return {
                'type': 'ir.actions.act_url',
                'url': '/web/binary/saveas?model=export_invoice.wizard&field=txt_file&id=%s&filename_field=invoices.txt' % (
                wizard.id),
                'target': 'self',
            }

A:

I believe the problem might be in the line
wizard = self.env['export_invoice.wizard'].with_context({'active_id': self.id}).create({})

If the idea is to add the specified context to the wizard without otherwise modifying it, do not add .create, which called with an empty dict will create and return an empty wizard record, which does not have contents in the file field, hence the 404.
wizard = self.env['export_invoice.wizard'].with_context({'active_id': self.id})

On a side note, the filename_field GET parameter expects a model field name that contains the file name, not the actual file name. This should not break the download, but the download file name will be generated, not the one specified.

