1.下载页面
2.下载页面代码
def file_iterator(file_name, chunk_size=1024):with open(file_name) as f:while True:c = f.read(chunk_size)if c:yield celse:breakdef FileDownload(request):if request.method == 'GET':FilePath = request.GET['filepath']FileName = str(FileInfo.objects.get(FileField=FilePath).FileName)response = StreamingHttpResponse(file_iterator(FilePath))response['Content-Type'] = 'application/octet-stream'response['Content-Disposition'] = 'attachment;filename=%s' % FileNamereturn response# print FilePathelse:return HttpResponse('method must be get')
<a name="Hv05L"></a># url` url(r'^download/', views.FileDownload), `<a name="cChkE"></a>##### 3.下载页面前端代码```html<div class="panel-body"><table class="table File-List-index-Table"><tr><th>Id</th><th>项目</th><th>名称</th><th>备注</th><th>上传时间</th><th>下载</th></tr>{% if list == '' %}<span></span>{% else %}{% for m in list %}<tr><td>{{ m.id }}</td><td>{{ m.ProName }}</td><td>{{ m.Filename }}</td><td>{{ m.Content }}</td><td><script>document.write((new Date({{ m.UpdateTime }} * 1000).toLocaleString()))</script></td><td><a href="/download/?filepath={{ m.FileField }}">下载</a></td></tr>{% endfor %}{% endif %}</table></div>
作者:Leebor
链接:https://www.jianshu.com/p/147d05f5ab96
来源:简书
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

