从django don下载的文件';没有文件';s扩展

从django don下载的文件';没有文件';s扩展,django,Django,我正在我的Django 1.5project上写一个视图,让用户下载一个文件 代码如下: import mimetypes from django.http import HttpResponse def filedownload(request, file_name): down_file = File.objects.get(name = file_name) file_path = MEDIA_ROOT+str(down_file.file) #down_file.fil

我正在我的
Django 1.5
project上写一个视图,让用户下载一个文件

代码如下:

import mimetypes
from django.http import HttpResponse

def filedownload(request, file_name):
    down_file = File.objects.get(name = file_name)
    file_path = MEDIA_ROOT+str(down_file.file) #down_file.file is something like folder/name_file.extension
    response = HttpResponse(mimetype='application/force-download')
    response['Content-Disposition'] = 'attachment; filename=%s' % file_name
    response['X-Sendfile'] = file_path
    return response
它工作得很好,但是下载的文件没有扩展名。为什么?我怎样才能解决这个问题?我知道我可以让Web服务器这样做,但这是一个虚拟项目,只能在Django中工作

编辑:
感谢sk1p的回答,我解决了这个问题,并使用了一个更为复杂的代码,您正在使用以下行指定要在浏览器中显示的文件名:

response['Content-Disposition'] = 'attachment; filename=%s' % file_name

因此,如果
file\u name
不包含扩展名,则下载也不会包含扩展名。因此:确保
内容处置
标题包含正确的文件名和扩展名

您正在使用以下行指定要在浏览器中显示的文件名:

response['Content-Disposition'] = 'attachment; filename=%s' % file_name

因此,如果
file\u name
不包含扩展名,则下载也不会包含扩展名。因此:确保
内容处置
标题包含正确的文件名和扩展名

file\u name
是否包含扩展名?确保
X-Sendfile
头没有做任何奇怪的事情。据我所知,它告诉Web服务器(希望不是Django的
runserver
)发送您指定的文件的内容。可能Web服务器正在添加自己的
内容配置
标题,或者更改或删除您的标题?
文件名
是否包含扩展名?请确保
X-Sendfile
标题没有做任何奇怪的事情。据我所知,它告诉Web服务器(希望不是Django的
runserver
)发送您指定的文件的内容。可能Web服务器正在添加自己的
内容处置
标题,或者更改或删除您的标题?还可能希望引用文件名,以防HTTP标题可能会误解其空格或其他字符(分号?)。还可能希望引用文件名,以防其包含空格或其他字符(分号?)HTTP头可能会误解。