Created
June 26, 2020 04:49
-
-
Save sehrishnaz/cb6401882bf9bb37b0712eed400619e3 to your computer and use it in GitHub Desktop.
Write binary data into zip file and downlaod it on button click in odoo
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@api.multi | |
def odoo_button_click(self): | |
print('****************odoo_button_click******************') | |
import os, zipfile | |
# function to convert binary data | |
def isBase64_decodestring(s): | |
try: | |
return base64.decodestring(s) | |
except Exception as e: | |
raise ValidationError('Error:', +str(e)) | |
# getting working module where your current python file (model.py) exists | |
path = os.path.dirname(os.path.realpath(__file__)) | |
# creating dynamic path to create zip file | |
file_name = "static\\src\\any_folder\\" + str(self.field_1.name.strip().replace(' ', '_').replace('-', '_')) + '_' + str(self.name.strip().replace(' ', '_')) | |
file_name_zip = file_name+".rar" | |
zipfilepath = os.path.join(path, file_name_zip) | |
# creating zip file in above mentioned path | |
zip_archive = zipfile.ZipFile(zipfilepath, "w") | |
# creating file name (like example.txt) in which we have to write binary field data or attachment | |
object_name = self.binary_field_name | |
object_handle = open(object_name, "w") | |
# writing binary data into file handle | |
object_handle.write(isBase64_decodestring(self.attachment)) | |
object_handle.close() | |
# writing file into zip file | |
zip_archive.write(object_name) | |
zip_archive.close() | |
# code snipet for downloading zip file | |
return { | |
'type': 'ir.actions.act_url', | |
'url': str('/your_module/static/src/any_folder/'+str(zip_file_name_with_rar_extension), | |
'target': 'new', | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
For details: http://learnopenerp.blogspot.com/2020/06/write-binary-data-nto-zip-file-and-downlaod-it-on-button-click-in-odoo.html