2017-05-10 11 views
1

장고 앱에서 다운로드 파일을 제공하려고합니다. 아래 코드를 작성했지만 한 가지 문제가 있습니다. 파일이 다운로드되는 대신 브라우저에서 열립니다. .장고로 다운로드 할 파일을 제공합니다

내 응용 프로그램에 제공되는 파일은 exe이고 열 때 임의의 문자입니다.

그렇다면 파일을 열지 않고 다운로드하려면 어떻게 지정해야합니까? 당신은 내용 - 처리 헤더를 설정할

with open(path_to_apk, 'rb') as fh: 
    response = HttpResponse(fh) 
    response['Content-Disposition'] = 'inline; filename=' + os.path.basename(path_to_apk) 
    return response` 
+0

가능한 중복 (http://stackoverflow.com/questions/908258/generating-file-to-download-with-django는) – DhiaTN

답변

1

감사에 "첨부 파일"(너무 적절한 콘텐츠 유형을 설정 - var에 이름에서 내가 적절한 교체 다른, 해당 파일은 안드로이드 패키지입니다 가정 콘텐츠 형식) : 장고 다운로드 파일 생성]의

response = HttpResponse(fh, content_type="application/vnd.android.package-archive") 
response["Content-disposition"] = "attachment; filename={}".format(os.path.basename(path_to_apk)) 
return response 
+0

감사이 보인다 코드에 철자 오류가 있습니다 : 포맷 대신에 fomat을 쓰면 솔루션을 사용하고자하는 다른 사람들을 위해 수정할 수 있습니다. –

+0

@AbderrahmanGourragui 오타가 고정되었습니다. –

관련 문제