【发布时间】:2017-11-17 14:57:08
【问题描述】:
我有一个 Django 视图 (download-attachment),它返回一个 django.http.response.HttpResponse 对象。
对象的字典表示是:
{
'reason_phrase': u'OK',
'_handler_class': None,
'_headers': {'content-length': ('Content-Length', '21'),
'content-type': ('Content-Type', 'text/plain'),
'content-disposition': ('Content-Disposition', 'attachment;
filename="upload_file.txt"')
},
'_charset': None,
'_closable_objects': [],
'cookies': <SimpleCookie: >,
'closed': False,
'_container': ['Upload to file\n']
}
在模板中,点击超链接即可呈现视图:
<a href="{% url "download-attachment" certificationID=certificationID fileID=attachment.id %}" download> {{attachment.name}}</a>
这里的certificateID和fileID是下载附件视图的url参数。
在 chrome 中,单击超链接时,文件将作为附件下载,文件名在响应的 Content-Disposition 标头中给出。
在 Firefox 中,文件下载失败。需要帮助在 Firefox 中进行文件下载。
【问题讨论】:
-
它失败了如何?你在 Django 中做什么来提供这些内容?
-
@DanielRoseman 在 Django 中,我正在创建一个 django.http.response.HttpResponse 类的对象。并从视图中返回该对象。
response = HttpResponse(data, content_type="text/plain") response["Content-Description"] = "File Transfer" response["Content-Disposition"] = "attachment; filename=\"%s\"" % self.cweResponse.get("file_name") response['Content-Length'] = self.cweResponse.get("size") return response在Firefox中,下载列表显示文件名失败,无法打开。
标签: html django django-templates