Search code examples
odooodoo-14

How to upload apk file as binary field and download it with same file name in odoo


I need a form which accept upload android .apk file. and download it with same name.

Here is what I start with

# model.py
class AppVersion(models.Model):
  # ...
  apk_file = fields.Binary('Apk file', filters='*.apk', required=True)

But in odoo-14 the filters param is not work any more. It accept all kind files.

odoo form accept all kind files

And after upload complete, download button gives me a zip file.

enter image description here


Solution

  • After some source code research I found the solution:

    Issues 1: accpet .apk file only

    Add a options attribute with accepted_file_extensions property into the binary field.

    Issues 2: download file with same name

    Add a new char field in model to save the file name, bind it to binary field with filename attribute. and don't forget add the new field into form view (set to invisible)

    Code

    # model
    class AppVersion(models.Model):
      # ...
      apk_file = fields.Binary('Apk file', store=True, attachment=False, required=True)
      apk_fname = fields.Char(string='File name')
    
    <!--form view-->
    <field name="apk_file" widget="binary" options="{'accepted_file_extensions': '.apk'}" filename="apk_fname"/>
    <field name="apk_fname" invisible="1"/>
    

    Success result

    binary field with file name