Last active
June 27, 2022 16:03
-
-
Save reidransom/a397cd2fb7d489ef3049 to your computer and use it in GitHub Desktop.
Returning binary data from a Django view
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
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