Forked from viveksoundrapandi/python django create and download zip
Last active
June 10, 2024 23:11
-
-
Save Jazzis18/28a959471c2c50bf0b680e3da36d247c to your computer and use it in GitHub Desktop.
Revisions
-
Jazzis18 revised this gist
Jan 12, 2017 . 2 changed files with 19 additions and 17 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -1,17 +0,0 @@ This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,19 @@ from shutil import make_archive from wsgiref.util import FileWrapper def download(request, file_name=""): """ A django view to zip files in directory and send it as downloadable response to the browser. Args: @request: Django request object @file_name: Name of the directory to be zipped Returns: A downloadable Http response """ files_path = "/tmp/albums/" path_to_zip = make_archive(files_path, "zip", files_path) response = HttpResponse(FileWrapper(open(path_to_zip,'rb')), content_type='application/zip') response['Content-Disposition'] = 'attachment; filename="{filename}.zip"'.format( filename = file_name.replace(" ", "_") ) return response -
viveksoundrapandi created this gist
Mar 17, 2014 .There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,17 @@ from shutil import make_archive from django.core.servers.basehttp import FileWrapper def download(request,file_name=""): """ A django view to zip files in directory and send it as downloadable response to the browser. Args: @request: Django request object @file_name: Name of the directory to be zipped Returns: A downloadable Http response """ file_path = "/tmp/albums/"+file_name path_to_zip = make_archive(file_path,"zip",file_path) response = HttpResponse(FileWrapper(file(path_to_zip,'rb')), content_type='application/zip') response['Content-Disposition'] = 'attachment; filename='+file_name.replace(" ","_")+'.zip' return response