我看了很多相同的问题,但没有一个能解决我的问题, 我正在尝试使用按钮下载在我的 View 中生成的文件。 问题:单击下载按钮时我得到一个空文件
我的看法:
class dumpView(View):
template_name = 'download.html'
def get(self, request):
file = open("/home/test.pdf", "rwbt")
response = HttpResponse(file.read(), content_type="application/pdf")
response['Content-Disposition'] = 'attachement; filename=%s' % file
return render(request, 'download.html')
我的网址:
url(r'^dump/', dumpView.as_view()),
我的模板:
{% extends 'base.html' %}
{% load staticfiles %}
{% block content %}
<div class="container">
<div class="row">
<div class="jumbotron">
<div class="row">
<center>
<a href="javascript:void(0)" class="btn btn-primary btn-lg dump">Download</a>
</center>
</div>
</div>
</div>
</div>
{% endblock %}
{% block javascript %}
<script src="{% static 'js/home.js' %}"></script>
{% endblock %}
我的 Django View 正在呈现响应。我确实在 UI 中使用类转储向按钮添加了一个点击处理程序,以向“/dump”URL 发出 XHR 请求并接收响应作为 blob,并下载具有自定义名称和日期的文件。
我的代码:
$(".dump").on("click", function() {
xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
var a, today;
if (xhttp.readyState === 4 && xhttp.status === 200) {
a = document.createElement('a');
a.href = window.URL.createObjectURL(xhttp.response);
today = new Date();
a.download = "file_" + today.toDateString().split(" ").join("_") + ".pdf";
a.style.display = 'none';
document.body.appendChild(a);
return a.click();
}
};
xhttp.open("GET", "/dump", true);
xhttp.setRequestHeader("Content-Type", "application/json");
xhttp.responseType = 'blob';
xhttp.send();
});
最佳答案
在您看来,我建议在您的回复中增加尺寸
content = file(filename).read()
response = HttpResponse(content, content_type='text/plain')
response['Content-Length'] = os.path.getsize(filename)
response['Content-Disposition'] = 'attachment; filename=%s' % 'my_pdf.pdf'
正如@bob-vork 刚才所说,Content-Disposition
中的文件名不应该是 python file
对象,而是您下载的名称。
如何在 urls.py 中注册 View ,然后将 javascript 替换为:
<input type="button" value="Download" onclick="window.open('download_my_pdf')">
编辑:
既然您要使用建议的 HTML,那么我会在我的解决方案中添加更多细节。
首先,您可以删除 home.js
。正如你所理解的
<a href="javascript:void(0)" class="btn btn-primary btn-lg dump">Download</a>
成为
<input type="button" value="Download" onclick="window.open('download_my_pdf')">
你的views.py
应该直接返回response
from django.http import HttpResponse
from wsgiref.util import FileWrapper
@login_required
def download_pdf(request):
filename = 'whatever_in_absolute_path__or_not.pdf'
content = FileWrapper(filename)
response = HttpResponse(content, content_type='application/pdf')
response['Content-Length'] = os.path.getsize(filename)
response['Content-Disposition'] = 'attachment; filename=%s' % 'whatever_name_will_appear_in_download.pdf'
return response
也要注意放置正确的content_type。请注意,我没有测试 View (尤其是 FileWrapper),我改编了一些用于纯文本的代码。这也是将 pdf 视为文本文件时出现 TypeError 的原因。
在 urls.py 中
urlpatterns = (
[...]
url('download_my_pdf', download_pdf),
)
关于javascript - 使用 xmlhttprequest 使用按钮在 django 中下载文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44905660/