Html 从Django模板下载文件作为附件在firefox中不起作用

Html 从Django模板下载文件作为附件在firefox中不起作用,html,django,django-templates,Html,Django,Django Templates,我有一个Django视图(下载附件),它返回一个Django.http.response.HttpResponse对象 对象的字典表示形式为: { 'reason_phrase': u'OK', '_handler_class': None, '_headers': {'content-length': ('Content-Length', '21'), 'content-type': ('Content-Type', 'text/

我有一个Django视图(下载附件),它返回一个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']
}
{
    'reason_phrase': u'OK', 
    '_handler_class': None, 
    '_headers': {'content-length': ('Content-Length', '21'), 
                 'content-type': ('Content-Type', 'text/plain'),
                 'content-encoding': ('Content-Encoding', 'None'),
                 'content-disposition': ('Content-Disposition', 'attachment; 
    filename="upload_file.txt"')
                }, 
    '_charset': None, 
    '_closable_objects': [], 
    'cookies': <SimpleCookie: >, 
    'closed': False, 
    '_container': ['Upload to file\n']
}
{
“原因”——“好的”,
“\u处理程序\u类”:无,
“_headers':{'content-length':('content-length','21'),
'内容类型':('content-type','text/plain'),
'内容处置':('content-disposition','附件;
filename=“upload_file.txt””)
}, 
“_字符集”:无,
“\u可关闭的\u对象”:[],
“饼干”:,
“关闭”:错误,
“\u容器”:[“上载到文件\n']
}
在模板中,单击超链接即可呈现视图:

<a href="{% url "download-attachment" certificationID=certificationID fileID=attachment.id %}" download> {{attachment.name}}</a>

这里certificationID和fileID是下载附件视图的url参数

在chrome中,单击超链接后,文件将作为附件下载,文件名在响应的Content Disposition标头中给出


在firefox中,文件下载失败。需要帮助在firefox中下载文件。

不是客户端或模板问题。Firefox需要在响应头中设置内容编码实体来下载文件附件。即使没有编码,也需要设置标头。添加了对Django 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']
}
{
    'reason_phrase': u'OK', 
    '_handler_class': None, 
    '_headers': {'content-length': ('Content-Length', '21'), 
                 'content-type': ('Content-Type', 'text/plain'),
                 'content-encoding': ('Content-Encoding', 'None'),
                 'content-disposition': ('Content-Disposition', 'attachment; 
    filename="upload_file.txt"')
                }, 
    '_charset': None, 
    '_closable_objects': [], 
    'cookies': <SimpleCookie: >, 
    'closed': False, 
    '_container': ['Upload to file\n']
}
{
“原因”——“好的”,
“\u处理程序\u类”:无,
“_headers':{'content-length':('content-length','21'),
'内容类型':('content-type','text/plain'),
“内容编码”:(“内容编码”,“无”),
'内容处置':('content-disposition','附件;
filename=“upload_file.txt””)
}, 
“_字符集”:无,
“\u可关闭的\u对象”:[],
“饼干”:,
“关闭”:错误,
“\u容器”:[“上载到文件\n']
}

它怎么会失败?你在Django做什么来提供这些内容呢?@DanielRoseman在Django,我正在创建一个Django.http.response.HttpResponse类的对象。并从视图中返回该对象
response=HttpResponse(data,content\u type=“text/plain”)response[“content Description”]=“File Transfer”response[“content Disposition”]=“attachment;filename=\%s\”“%self.cweResponse.get(“File\u name”)response['content-Length']=self.cweResponse.get(“size”)在Firefox中返回响应
,下载列表显示文件名失败,无法打开。