2👍
✅
You want to set the Content-disposition header to “attachment” (and set the proper content-type too – from the var names I assume those files are android packages, else replace with the proper content type):
response = HttpResponse(fh, content_type="application/vnd.android.package-archive")
response["Content-disposition"] = "attachment; filename={}".format(os.path.basename(path_to_apk))
return response
0👍
First of all you have to set the Content_Disposition
header to attachment
.
And instead of worrying about correct value for Content_Type
header, use FileResponse
instead of HttpResponse:
file=open(path_to_apk,"rb")
response=FileResponse(file)
response['Content-Disposition'] = 'attachment; filename={}'.format(os.path.basename(path_to_apk))
return response
- [Answered ]-Django models.ForeignKey issue with model with custom foreign key
- [Answered ]-Django admin page adding save as pdf button
- [Answered ]-Django: CSS and Images (Static Files) loaded successfully but not applied
- [Answered ]-Creating file and saving in Django model (AttributeError?)
- [Answered ]-Docker: How to rely on a local db rather than a db docker container?
0👍
You can use a FileResponse with as_attachment=True
for this. It will handle stuff like reading the file into memory, setting the correct Content-Type
, Content-Disposition
, etc. automatically for you.
from django.http import FileResponse
def download_file(request):
return FileResponse(open(path_to_apk, 'rb'), as_attachment=True)
👤F30
- [Answered ]-How to use checkboxes in Django Admin for a ManyToMany field through a model
- [Answered ]-Send weekly emails for a limited time after sign up in django
- [Answered ]-Django REST – create object with PrimaryKey
Source:stackexchange.com