2013-12-10 103 views
0

我使用一個簡單的視圖類來強制下載文件的pk。文件下載名稱出來是PK。我希望它是原始文件名,就像它在服務器上一樣。 我該怎麼做?Django下載的文件名

*進出口新的Django的

這裏的視圖的代碼

class GetFileContent(View): 
def get(self,request, userName, pk): 
    user = User.objects.filter(username = userName) 
    filtered = File.objects.filter(pk = pk, published=True, file_owner = user) 
    data = filtered[0].content 
    return HttpResponse(data, content_type='application/force-download') 
    pass 
+0

除了Simeons的回答,我還建議通過mod_xsendfile傳遞文件,如果你使用apache並有root權限來配置它。 – Jingo

+0

我使用Amazon s3 –

回答

4

您需要設置文件名的響應:

response = HttpResponse(data, content_type='application/force-download') 
response['Content-Disposition'] = 'attachment; filename="{}"'.format(filename) 
return response 

你需要想出一個辦法以確定所需的文件名(例如,通過將其存儲在數據庫中以及查找)。