上篇有【文件上传】,算是完成了一个比较完整的项目。
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 c
else:
break
def 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' % FileName
return response
# print FilePath
else:
return HttpResponse('method must be get')
4.URL配置
url(r'^download/', views.FileDownload),
3.下载页面前端代码
Id |
项目 |
名称 |
备注 |
上传时间 |
下载 |
{% if list == '' %}
{% else %}
{% for m in list %}
{{ m.id }} |
{{ m.ProName }} |
{{ m.Filename }} |
{{ m.Content }} |
|
下载 |
{% endfor %}
{% endif %}