from uploads.models import Wanted
from django.http import FileResponse, Http404
def download(request, pk):
"""Download A File""
# Retrieve the file
wanted = Wanted.objects.filter(pk=pk)
if wanted.exists():
wanted = wanted.first()
# confirm that user has authority to download file
if wanted.ocassionfk.owner == request.user:
file_name = str(wanted.archive)
file_path = settings.MEDIA_ROOT + "/" + file_name
response = FileResponse(open(file_path, "rb"), as_attachment=True)
return response
return Http404()
The above view is offering me 3hrs to download a file of 250MB. Something is wrong. Why is django serving 250Mb file using 2-3hrs?
In addition to the above phenomenon, I am unable to resume downloads when they fail midway. This is frustrating. While I am suspecting the webserver too, I wonder if there is something I should do on the Django end to guarantee the resumption of downloads at least
The slowness might be the server. It might also be the ISP. You haven’t provided any information about your deployment environment so we can’t tell what the source of the delays may be.
I don’t know how browsers handle requests to resume a download, I’d guess they would issue a byte-range request. (I’ve never bothered to look.) If that’s the case, then you would need to handle the range header to determine where to resume the transfer.
However, this is one of those cases where I think you’d really be better off if you didn’t handle it within Django. If I had to approach something like this, I’d return a url that referenced the file and allow the file server to handle it like any other static or media file. We don’t use Django for file transfers - nginx is far superior for those functions (including knowing how to handle the range headers).
Added the following to nginx location context:
proxy_force_ranges on
and added the following to the above download()
view:
response['Accept-Ranges'] = 'bytes'
Download now resumes. Speeds are still ridiculous sometimes … investigating
I have come to the conclusion that the slowness is from my ISP. Sometimes it si fast, sometimes not.