Skip to content

Instantly share code, notes, and snippets.

Forked from reidransom/
Created October 8, 2019 14:55
What would you like to do?
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 =
filename = 'some-filename.xlsx'
response = HttpResponse(mimetype="application/ms-excel")
response['Content-Disposition'] = 'attachment; filename=%s' % filename # force browser to download file
return response
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment