Python csv和其他扩展的HttpResponse内容类型不';不适用于firefox,但适用于Chrome和IE

Python csv和其他扩展的HttpResponse内容类型不';不适用于firefox,但适用于Chrome和IE,python,django,content-type,httpresponse,Python,Django,Content Type,Httpresponse,我正在使用PYTHON+DJANGO实现一个文件共享系统。当用户试图下载一个文件时,它在Chrome和IE中运行良好,但在Firefox中不运行,Firefox返回部分文件名,如果不识别扩展名(例如.pl和.csv),则不返回扩展名 看法 我尝试了content\u type=mimetypes.guess\u type(文件名),但这并没有解决问题 我还尝试用句点替换文件名中的空格,这确实有效!但我确信有一个干净的解决方案 基于django.views.static: import mimet

我正在使用PYTHON+DJANGO实现一个文件共享系统。当用户试图下载一个文件时,它在Chrome和IE中运行良好,但在Firefox中不运行,Firefox返回部分文件名,如果不识别扩展名(例如.pl和.csv),则不返回扩展名

看法

我尝试了content\u type=mimetypes.guess\u type(文件名),但这并没有解决问题
我还尝试用句点替换文件名中的空格,这确实有效!但我确信有一个干净的解决方案

基于django.views.static:

import mimetypes
import os
import stat
from django.http import HttpResponse

statobj = os.stat(fullpath)
mimetype, encoding = mimetypes.guess_type(fullpath)
mimetype = mimetype or 'application/octet-stream'

with open(fullpath, 'rb') as f:
    response = HttpResponse(f.read(), mimetype=mimetype)

if stat.S_ISREG(statobj.st_mode):
    response["Content-Length"] = statobj.st_size
if encoding:
    response["Content-Encoding"] = encoding
response['Content-Disposition'] = 'inline; filename=%s'%os.path.basename(fullpath)
return response

我知道,这是一个老问题的答案,但实际问题是您没有用双引号括起文件名(而且必须是双引号,而不是单引号)。IE和Chrome将一直读到最后一行,而Firefox将一直读到第一个空格并停止


所以只需将
response['Content-Disposition']=“附件;filename=“+entry.name
更改为
response['Content-Disposition']=”附件;filename=“%s””%(entry.name)
您已经设置好了。

这与我的解决方法相同(用句点替换空格),因为在下载时空格被下划线替换。我正在尝试允许文件在不更改名称的情况下下载。但是谢谢你的帮助:)
import mimetypes
import os
import stat
from django.http import HttpResponse

statobj = os.stat(fullpath)
mimetype, encoding = mimetypes.guess_type(fullpath)
mimetype = mimetype or 'application/octet-stream'

with open(fullpath, 'rb') as f:
    response = HttpResponse(f.read(), mimetype=mimetype)

if stat.S_ISREG(statobj.st_mode):
    response["Content-Length"] = statobj.st_size
if encoding:
    response["Content-Encoding"] = encoding
response['Content-Disposition'] = 'inline; filename=%s'%os.path.basename(fullpath)
return response