1.下载页面

文件下载 - 图1
image.png

2.下载页面代码
  1. def file_iterator(file_name, chunk_size=1024):
  2. with open(file_name) as f:
  3. while True:
  4. c = f.read(chunk_size)
  5. if c:
  6. yield c
  7. else:
  8. break
  9. def FileDownload(request):
  10. if request.method == 'GET':
  11. FilePath = request.GET['filepath']
  12. FileName = str(FileInfo.objects.get(FileField=FilePath).FileName)
  13. response = StreamingHttpResponse(file_iterator(FilePath))
  14. response['Content-Type'] = 'application/octet-stream'
  15. response['Content-Disposition'] = 'attachment;filename=%s' % FileName
  16. return response
  17. # print FilePath
  18. else:
  19. return HttpResponse('method must be get')
  1. <a name="Hv05L"></a>
  2. # url
  3. ` url(r'^download/', views.FileDownload), `
  4. <a name="cChkE"></a>
  5. ##### 3.下载页面前端代码
  6. ```html
  7. <div class="panel-body">
  8. <table class="table File-List-index-Table">
  9. <tr>
  10. <th>Id</th>
  11. <th>项目</th>
  12. <th>名称</th>
  13. <th>备注</th>
  14. <th>上传时间</th>
  15. <th>下载</th>
  16. </tr>
  17. {% if list == '' %}
  18. <span></span>
  19. {% else %}
  20. {% for m in list %}
  21. <tr>
  22. <td>{{ m.id }}</td>
  23. <td>{{ m.ProName }}</td>
  24. <td>{{ m.Filename }}</td>
  25. <td>{{ m.Content }}</td>
  26. <td>
  27. <script>
  28. document.write((new Date({{ m.UpdateTime }} * 1000).toLocaleString()))
  29. </script>
  30. </td>
  31. <td><a href="/download/?filepath={{ m.FileField }}">下载</a></td>
  32. </tr>
  33. {% endfor %}
  34. {% endif %}
  35. </table>
  36. </div>

作者:Leebor
链接:https://www.jianshu.com/p/147d05f5ab96
来源:简书
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。