Hilfe

2

How to download attachment on button click action

Avatar
Anuj Thakur

I Have attached Some files using document management module in one Model. I need to open that attachment on button click in another model

Below code is for reference.

XML Code:-

<button string="System Help" type="object" class="oe_finesse_highlight" name="emp_system_help"/>

.py Code:-

def emp_system_help(self, cr, uid, ids, context=None):

      //Need action to download attachment which is attached using Document management Module                                              return


Thanks in advance.

1 Kommentar
Avatar
Verwerfen
3 Antworten
4
Avatar
Axel Mendoza
Best Answer

Very interesting question, that allow me to give some light to a couple of hidden features of Odoo that allow you to do exactly that using the report action like this:

    @api.multi
    def emp_system_help(self):
        return {
            'type': 'ir.actions.report.xml',
            'report_type': 'controller',
//for v9 and v10
            'report_file': '/web/content/res.partner/7/image/axel.png?download=true',
// for v7 and v8
// 'report_file': '/web/binary/saveas?model=ir.attachment&field=datas&filename_field=datas_fname&id=194
 }

Just need to return the action definition of type ir.actions.report.xml and report_type controller that will allow you to download any kind of file from any URL that you could specify in the report_file of the action dict

As you could see the format of the report_file URL contains the information referring to the res.partner model and record id=7 and the field image of that record to be downloaded and axel.png as the filename of the image file to be downloaded.

This means that you could build URLs to access the binary field values of any model in your Odoo instance. Here I left you the complete formats supported by the /web/content controller routes as states the method definition with it's default argument values

For v9 and v10

@http.route(['/web/content',
'/web/content/<string:xmlid>',
'/web/content/<string:xmlid>/<string:filename>',
'/web/content/<int:id>',
'/web/content/<int:id>/<string:filename>',
'/web/content/<int:id>-<string:unique>',
'/web/content/<int:id>-<string:unique>/<string:filename>',
'/web/content/<string:model>/<int:id>/<string:field>',
'/web/content/<string:model>/<int:id>/<string:field>/<string:filename>'], type='http', auth="public")
def content_common(self, xmlid=None, model='ir.attachment', id=None, field='datas', filename=None, filename_field='datas_fname', unique=None, mimetype=None, download=None, data=None, token=None):

Hope this helps b


1 Kommentar
Avatar
Verwerfen
Avatar
Anuj Thakur
-

Thanks a lot. Its working fine

2
Avatar
Cyrus Waithaka
Best Answer

This also works on v11

{
'name': 'Report',
'type': 'ir.actions.act_url',
'url': "web/content/?model=" + self._name +"&id=" + str(
self.id) + "&filename_field=file_name&field=data_file&download=true&filename=" + self.file_name,
'target': 'self',
}
Avatar
Verwerfen
1
Avatar
Marwa ROMDHAN
Best Answer
V11 to download a file:
@api.one
def _get_template(self):
self.contract_template = base64.b64encode(open("/path/to/the/file/contract_template.xls", "rb").read())

contract_template = fields.Binary('Template', compute="_get_template")

@api.multi
def get_contract_template(self):
return {
'type': 'ir.actions.act_url',
'name': 'contract',
'url': '/web/content/model.name/%s/contract_template/contract_template.xls?download=true' %(self.id),
}

XML:
<button name="get_contract_template" string="Download Template" type="object" class="oe_link oe_right"/>
Avatar
Verwerfen