Search Here

How to Create Image or File Upload Binary Field in Odoo

In this post you will learned about binary fields in Odoo. You will also learned how to create and use binary fields or save images in Odoo form view or odoo reports like QWEB reports. 

What are Binary Fields in Odoo

       Binary Fields Stores file, encoded in base64 in bytea column in your created table. For example I have a table and the last column of the table shows binary data.

In the below screen shot you can see that column.

How to Create Image or File Upload (Binary) Field and Set Uploaded file name as the Orignal File on Download

 

How to create Binary Fields

If you want to create a binary field in existing model than you need to inherit already existing model. 


If you are going to use your new model than follow below steps to create file upload field (binary).

Creating File Upload (Binary) Field in Odoo

As you can see that I have created a model and two fields. One of the field is type Binary called "image", which will be used to upload images. And the other one is a Char type field named "image_file_name", the purpose of this field is to store the orignal file name in the database which we are going to upload. 

Now we will create form view to show the above created binary field.


The above code will create a form view which shows our created binary field to upload images or files. Below screen shot shows how created binary field looks like in from view.

How to Create Image or File Upload (Binary) Field and Set Uploaded file name as the Orignal File on Download

Now select an Image. This stored image file encoded in base64 in bytea column in your created table. 

How to show uploaded images in Qweb Reprots

        Once the images has been uploaded its time to show in qweb reports. For that purpose you have to follow these stpes. The below code show the xml file for creating qweb reports in odoo. In this file we will call our image field which one is uploaded from form view of odoo.

The below line of code is the key part of showing binary field (image) in qweb reports or your template.

<img t-attf-src="data:image/jpg;base64,{{ your_model_name.binary_field_name }}" style="width:95px; height:95px;margin-left:55px;"/>

Here "your_model_name" is the name of your model or object for example "(my.model(4,))" , and "binary_field_name" is the name of the "Binary Field" created in odoo model. In our case its "my_images".

The above code will print the images in the original format, we can change the size of image simply applying by some css. You simply need to call the "t-attf-src" and you can print them out.

Post a Comment

9 Comments

  1. Thank you for this example! how can this be applied to a txt file, as simple as uploading the file and the procesess the content in the python model?

    ReplyDelete
  2. is there any methods to upload pdf and text documents

    ReplyDelete
    Replies
    1. yes you can do this by using binary fields. by using binary fields you could upload any file (xlxs,txt etc)

      Delete
    2. @api.multi
      def btn_create_s(self,context=None):
      dir = os.path.dirname(__file__)
      timestemp = int(time.time())
      file_name = str(self._uid)+"_"+str(timestemp)
      file_path = dir+"/temp_"+file_name+".xlsx"
      print('---------------------',file_path)
      if self.attachment:
      print('************',self.store_fname)
      decoded_data = base64.b64decode(self.attachment)
      xls_filelike = io.BytesIO(decoded_data)
      print('---------------------1',xls_filelike)
      wb = openpyxl.load_workbook(xls_filelike)
      print('---------------------2',wb)

      This code is for excel file

      Delete
    3. In the above code self.attachment is a binary field.

      Delete
  3. How can I embed in a qweb-report a pdf that I received in bytes by request that I made to a webservice?

    ReplyDelete
  4. hello! how to make join files in a custom module, i have to use ir.atachement or a field.binary?
    and most importantly how to do it please I want to have a button attachment files in form view!
    thank you for your answers

    ReplyDelete
    Replies
    1. can you please tell me what is join files?????????

      Delete