下載文件,無法正常打開,如下圖
需手動(dòng)加后綴名修改文件格式方可正常打開
進(jìn)而能得知文件內(nèi)容正常
如下圖,給“下載”文件加上后綴即可得到正常下載的內(nèi)容
1、已在源碼中指定了下載的文件名:
response['Content-Disposition'] = 'attachment;filename="{f_name}"'.format(f_name=the_file_name)
但結(jié)果下載的文件統(tǒng)一均為“下載”的文件名字
2、下載功能和數(shù)據(jù)內(nèi)容是正常的,應(yīng)該是命名這個(gè)環(huán)節(jié)出錯(cuò),并且文件是含有中文字符,請(qǐng)幫指正。
補(bǔ)充:
python版本 3.6
django版本 1.11.13
from django.http import StreamingHttpResponse
# 切片讀取文件
def file_iterator(filename,chunk_size=512):
with open(filename,'rb') as f:
while True:
c=f.read(chunk_size)
if c:
yield c
else:
break
# 下載功能
def download_file(request):
the_file_name = models.FileObj.objects.get(id=request.GET.get("id")).fileName # 顯示在彈出對(duì)話框中的默認(rèn)的下載文件名
file_path = os.path.join(file_dir,the_file_name) # 要下載的文件路徑
response = StreamingHttpResponse(file_iterator(file_path))
response['Content-Type'] = 'application/octet-stream'
response['Content-Disposition'] = 'attachment;filename="{f_name}"'.format(f_name=the_file_name)
return response
打開網(wǎng)頁檢查,如下圖
發(fā)現(xiàn)是Content-Disposition出錯(cuò)
接下來,就針對(duì)這個(gè)屬性在網(wǎng)上搜集資料,解決步驟如下:
1、導(dǎo)入模塊
from django.utils.encoding import escape_uri_path
2、重寫該屬性
response['Content-Disposition'] = "attachment; filename*=utf-8''{}".format(escape_uri_path(the_file_name))
注:其中BUG1和BUG2普通解決方案不是最佳解決途徑
from django.http import StreamingHttpResponse
# 切片讀取文件
def file_iterator(filename,chunk_size=512):
with open(filename,'rb') as f:
while True:
c=f.read(chunk_size)
if c:
yield c
else:
break
# 下載功能
def download_file(request):
the_file_name = models.FileObj.objects.get(id=request.GET.get("id")).fileName # 顯示在彈出對(duì)話框中的默認(rèn)的下載文件名
print(the_file_name)
file_path = os.path.join(file_dir,the_file_name) # 要下載的文件路徑
response = StreamingHttpResponse(file_iterator(file_path))
response['Content-Type'] = 'application/octet-stream' # #設(shè)定文件頭,這種設(shè)定可以讓任意文件都能正確下載,而且已知文本文件不是本地打開
# response['Content-Disposition'] = 'attachment;filename="download.zip"' # BUG1:給出一個(gè)固定的文件名,且不能為中文,文件名寫死了
# response['Content-Disposition'] = 'attachment;filename={0}'.format(the_file_name.encode("utf-8")) # BUG2:中文會(huì)亂碼
response['Content-Disposition'] = "attachment; filename*=utf-8''{}".format(escape_uri_path(the_file_name)) # 正確寫法
return response
正常下載zip文件的效果圖:
北大青鳥APTECH成立于1999年。依托北京大學(xué)優(yōu)質(zhì)雄厚的教育資源和背景,秉承“教育改變生活”的發(fā)展理念,致力于培養(yǎng)中國IT技能型緊缺人才,是大數(shù)據(jù)專業(yè)的國家
北大青鳥中博軟件學(xué)院創(chuàng)立于2003年,作為華東區(qū)著名互聯(lián)網(wǎng)學(xué)院和江蘇省首批服務(wù)外包人才培訓(xùn)基地,中博成功培育了近30000名軟件工程師走向高薪崗位,合作企業(yè)超4
中公教育集團(tuán)創(chuàng)建于1999年,經(jīng)過二十年潛心發(fā)展,已由一家北大畢業(yè)生自主創(chuàng)業(yè)的信息技術(shù)與教育服務(wù)機(jī)構(gòu),發(fā)展為教育服務(wù)業(yè)的綜合性企業(yè)集團(tuán),成為集合面授教學(xué)培訓(xùn)、網(wǎng)
達(dá)內(nèi)教育集團(tuán)成立于2002年,是一家由留學(xué)海歸創(chuàng)辦的高端職業(yè)教育培訓(xùn)機(jī)構(gòu),是中國一站式人才培養(yǎng)平臺(tái)、一站式人才輸送平臺(tái)。2014年4月3日在美國成功上市,融資1
曾工作于聯(lián)想擔(dān)任系統(tǒng)開發(fā)工程師,曾在博彥科技股份有限公司擔(dān)任項(xiàng)目經(jīng)理從事移動(dòng)互聯(lián)網(wǎng)管理及研發(fā)工作,曾創(chuàng)辦藍(lán)懿科技有限責(zé)任公司從事總經(jīng)理職務(wù)負(fù)責(zé)iOS教學(xué)及管理工作。
浪潮集團(tuán)項(xiàng)目經(jīng)理。精通Java與.NET 技術(shù), 熟練的跨平臺(tái)面向?qū)ο箝_發(fā)經(jīng)驗(yàn),技術(shù)功底深厚。 授課風(fēng)格 授課風(fēng)格清新自然、條理清晰、主次分明、重點(diǎn)難點(diǎn)突出、引人入勝。
精通HTML5和CSS3;Javascript及主流js庫,具有快速界面開發(fā)的能力,對(duì)瀏覽器兼容性、前端性能優(yōu)化等有深入理解。精通網(wǎng)頁制作和網(wǎng)頁游戲開發(fā)。
具有10 年的Java 企業(yè)應(yīng)用開發(fā)經(jīng)驗(yàn)。曾經(jīng)歷任德國Software AG 技術(shù)顧問,美國Dachieve 系統(tǒng)架構(gòu)師,美國AngelEngineers Inc. 系統(tǒng)架構(gòu)師。