2017-06-15 38 views
0

我有一个Django视图(下载附件),它返回一个django.http.response.HttpResponse对象。从Django模板作为附件下载文件在Firefox中不工作

对象的字典表示为:

{ 
    '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> 

这里certificationID和FILEID是用于下载的URL的参数 - 附属视图。

在chrome中,单击超链接时,文件将以附件的形式下载到响应的Content-Disposition标头中。

在firefox中,文件下载失败。需要帮助在Firefox中进行文件下载工作。

+0

它失败*如何*?你在做什么Django服务这个内容? –

+0

@DanielRoseman在Django中,我创建了一个django.http.response.HttpResponse类的对象。并从视图中返回该对象。 响应=“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中,下载列表显示文件名与失败,它无法打开。 –

回答

0

不是客户端或模板问题。 Firefox需要在响应头中设置的Content-Encoding实体来下载文件附件。即使没有编码,也需要设置标题。添加了内容编码Django HttpResponse对象。

对象的新词典表示为:

{ 
    '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'] 
}