⾸先在html中加个a标签
⽂件下载接着在项⽬cmdb的urls.py(注,分流的urls.py) 添加url(r'^file_down/', views.file_down),
接着在cmdb中的views.py添加 file_down的类
from django.http import StreamingHttpResponsedef file_down (request): #⽂本下载 import os
filename = os.path.join('filedown',\"test.txt\" ) # 要下载的⽂件路径 # do something
the_file_name = 'test.txt' # 显⽰在弹出对话框中的默认的下载⽂件名 response = StreamingHttpResponse(readFile(filename))
response['Content-Type'] = 'application/octet-stream' #表明他是⼀个字节流
response['Content-Disposition'] = 'attachment;filename=\"{0}\"'.format(the_file_name) #默认⽂件名 不过好像加不加都没什么关系 return response
def readFile(filename, chunk_size=512): with open(filename, 'rb') as f: while True:
c = f.read(chunk_size) if c:
yield c else: break效果图如下:
因篇幅问题不能全部显示,请点此查看更多更全内容