Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save Jazzis18/28a959471c2c50bf0b680e3da36d247c to your computer and use it in GitHub Desktop.

Select an option

Save Jazzis18/28a959471c2c50bf0b680e3da36d247c to your computer and use it in GitHub Desktop.

Revisions

  1. Jazzis18 revised this gist Jan 12, 2017. 2 changed files with 19 additions and 17 deletions.
    17 changes: 0 additions & 17 deletions python django create and download zip
    Original file line number Diff line number Diff line change
    @@ -1,17 +0,0 @@
    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

    19 changes: 19 additions & 0 deletions python django create and download zip.py
    Original 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

  2. @viveksoundrapandi viveksoundrapandi created this gist Mar 17, 2014.
    17 changes: 17 additions & 0 deletions python django create and download zip
    Original 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