Skip to content

Instantly share code, notes, and snippets.

@Rocckk
Forked from reidransom/views.py
Created October 8, 2019 14:55
Show Gist options
  • Save Rocckk/f90c13777b43be76747d4ab04f6e6965 to your computer and use it in GitHub Desktop.
Save Rocckk/f90c13777b43be76747d4ab04f6e6965 to your computer and use it in GitHub Desktop.
Returning binary data from a Django view
from django.http import HttpResponse
def django_file_download_view(request):
filepath = '/path/to/file.xlsx'
with open(filepath, 'r') as fp:
data = fp.read()
filename = 'some-filename.xlsx'
response = HttpResponse(mimetype="application/ms-excel")
response['Content-Disposition'] = 'attachment; filename=%s' % filename # force browser to download file
response.write(data)
return response
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment