Arghya Saha Arghya Saha -4 years ago 89
Python Question

Django download not working with browser, but works fine in machines where Internet Download Manager(IDM) is installed

My django project uses the following code to download the file. It works all good if the client machine has IDM installed but fails to work if IDM is not installed. I couldn't find any reason for this weirdness.

views.py

def somefunction():
something something
return render(request,
'something/download/download.html',
{'pdf_file_location': pdf_file_location})
def download(request):
if not request.user.is_authenticated():
return render(request, 'login/login/login.html')
else:
filename = request.POST.get('pdf_file_location')
if request.method == 'POST':
while os.path.exists(filename) is False:
time.sleep(2)
chunk_size = 8192
response = StreamingHttpResponse(FileWrapper(open(filename, 'rb'), chunk_size),
content_type=mimetypes.guess_type(filename)[0])
response['Content-Length'] = os.path.getsize(filename)
response['Content-Disposition'] = "attachment; filename=%s" % filename[filename.find("UserSessionDetails-")+19:]
return response
return render(request, 'something/something/index.html')


download.html

<canvas id="c-timer" width="300" height="300">
<input id="pdf_file_location" type="hidden" value={{ pdf_file_location }} name="pdf_file_location"/>
</canvas>


js for the download.html

var val = document.getElementById('pdf_file_location').value
data ={"pdf_file_location": val};
something something and then finishTime is called
var finishTime = function () {
$.post( "/book_publish/download/",data);
};


I don't have much knowledge about how IDM works, but reading this article tells me that it shouldn't give any upper hand apart from the fact that it opens multiple connections for the operation, and my code is sending data in chunks. Is it that the browser can't stitch the data when it is sent in small chunks?

Answer Source

PROBLEM: The problem was I was using JS to post a request for the download, and since I'm a newbie at web, so I couldn't handle the request sent back. Hence it got all messed up.

And somehow IDM was able to catch that response and initiate the download process.

SOLUTION: I used a simple form post and submit button in the HTML itself and not use the JS for post request.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download