Django 下载不适用于浏览器,但在安装了 Internet 下载管理器 (IDM) 的机器上运行良好
Django download not working with browser, but works fine in machines where Internet Download Manager(IDM) is installed
我的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 用于 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 告诉我,除了它为操作打开多个连接这一事实外,它不应该有任何优势,而且我的代码以块的形式发送数据。是不是浏览器在小块发送时无法拼接数据?
问题: 问题是我使用 JS 来 post 下载请求,因为我是网络新手,所以我不能' t 处理发回的请求。因此一切都搞砸了。
并且 IDM 能够以某种方式捕获该响应并启动下载过程。
解决方案: 我使用了一个简单的 表单 post 和 提交按钮 在 HTML 本身而不是将 JS 用于 post 请求。
我的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 用于 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 告诉我,除了它为操作打开多个连接这一事实外,它不应该有任何优势,而且我的代码以块的形式发送数据。是不是浏览器在小块发送时无法拼接数据?
问题: 问题是我使用 JS 来 post 下载请求,因为我是网络新手,所以我不能' t 处理发回的请求。因此一切都搞砸了。
并且 IDM 能够以某种方式捕获该响应并启动下载过程。
解决方案: 我使用了一个简单的 表单 post 和 提交按钮 在 HTML 本身而不是将 JS 用于 post 请求。