2
我正在編寫一個Django應用程序,讓您在滿足某些要求(例如,您必須登錄)後下載文件。該文件需要無法訪問,否則。通過Django中的視圖服務靜態文件
通過Apache服務文件不起作用:我必須在數據庫中檢查用戶的權限。此外,沒有權限更改我的Apache配置。
所以我想讀取Django中的文件,然後設置適當的標題並將其發送到客戶端。
我在Django手冊中使用了關於頁眉的this頁面的信息。
我有以下代碼:
#<- check persmissons here, continue if allowed ->
#read the pdf file
location = 'file.pdf'
file = open(location, 'r')
content = file.read()
file.close
#serve the file
response = HttpResponse(content, mimetype='application/pdf')
response['Content-Disposition'] = 'attachment; filename=filename.pdf'
return response
然而,下載的文件似乎已損壞:它不能在Adobe Reader中打開。 我在想,也許這是一些編碼問題,但我無法弄清楚。
任何幫助表示讚賞:)
這解決了我的問題,非常感謝! – Patrick 2010-08-21 22:17:30
或者你可以使用mod_xsendfile。 http://tn123.ath.cx/mod_xsendfile/ – 2010-08-21 22:20:24
謝謝,@Ignacio。我沒有意識到Apache世界的類似解決方案。 – 2010-08-21 22:27:16