2017-02-22 19 views
0

我的django項目使用下面的代碼來下載文件。如果客戶端計算機安裝了IDM,但它在IDM未安裝的情況下無法工作,則它工作良好。我找不到這種奇怪的原因。Django下載不支持瀏覽器,但在安裝Internet Download Manager(IDM)的機器上工作正常

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的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); 
     }; 

我沒有關於如何IDM工作多少知識,而是閱讀this article告訴我,它不應該給予任何優勢,它打開多個co該操作的nnections,我的代碼發送數據塊。是否瀏覽器在以小塊發送時無法拼接數據?

回答

0

問題:問題是我使用JS發佈下載請求,並且由於我是web上的新手,所以無法處理髮回的請求。因此,這一切都搞砸了。

以某種方式,IDM能夠捕獲該響應並啓動下載過程。

SOLUTION:我用一個簡單的形式後在HTML本身提交按鈕而不是使用JS的POST請求。

相關問題