Django下载不使用浏览器,但在安装了Internet Download Manager(IDM)的机器上运行良好

时间:2017-02-22 08:08:28

标签: python django browser client-server

我的django项目使用以下代码下载文件。如果客户端计算机已安装IDM,但如果未安装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 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);
      };

我对IDM的运作方式并不了解,但阅读this article告诉我,除了为操作打开多个连接之外,它不应该占上风。我的代码是以块的形式发送数据。浏览器是否可以在小块发送数据时拼接数据?

1 个答案:

答案 0 :(得分:0)

问题:问题是我使用JS发布下载请求,因为我是网络上的新手,所以我无法处理发回的请求。因此它搞得一团糟。

不知何故,IDM能够捕获该响应并启动下载过程。

解决方案:我在HTML中使用了一个简单的表单提交按钮,而不是使用JS进行帖子请求。

相关问题