2013-12-10 4 views
0

간단한보기 클래스를 사용하여 해당 파일을 "강제 다운로드"합니다. 파일 다운로드 이름이 pk로 나옵니다. 나는 그것이 서버에있는 것처럼 원본 파일 이름이되고 싶습니다. 어떻게하면됩니까? 여기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 Answer 외에도 아파치를 사용하고 루트 액세스 권한을 가지고 있다면 mod_xsendfile을 통해 파일을 전달하는 것이 좋습니다. – Jingo

+0

아마존 s3을 사용합니다. –

답변

4

:

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

당신은 방법을 알아낼해야합니다 원하는 파일 이름 (예를 들어, 데이터베이스에 조회를 저장함으로써)을 결정할 수 있습니다.

관련 문제