Content-disposition 是 MIME 协议的扩展,MIME 协议指示 MIME 用户代理如何显示附加的文件。
Content-disposition其实可以控制用户请求所得的内容存为一个文件的时候提供一个默认的文件名,文件直接在浏览器上显示或者在访问时弹出文件下载对话框。
Content-Disposition就是当用户想把请求所得的内容存为一个文件的时候提供一个默认的文件名。

import os,sys
from django.http import StreamingHttpResponse
from django.utils.encoding import escape_uri_path def file_iterator(file_name, chunk_size=):
with open(file_name,'rb') as f:
while True:
c = f.read(chunk_size)
if c:
yield c
else:
break def extractfile(request):
filepath="/".join(os.path.dirname(os.path.abspath(__file__)).split("/")[:-])+"/files"
the_file_name = filepath+"/Kafka权威指南.pdf"
response = StreamingHttpResponse(file_iterator(the_file_name))
response['Content-Type'] = 'application/octet-stream'
response['Content-Disposition'] = 'attachment;filename="{0}"'.format(escape_uri_path('Kafka权威指南.pdf')) return response

参考;

https://segmentfault.com/q/1010000009078463

https://www.jianshu.com/p/4c52cb691f54

https://cloud.tencent.com/developer/article/1365795

最新文章

  1. 使用OWIN作为WebAPI的宿主
  2. BZOJ 4582: [Usaco2016 Open]Diamond Collector
  3. 使用dd工具对磁盘RAID5和10进行I/O性能测试
  4. (转)浅谈ASP.NET报表控件
  5. DbUtility Ex
  6. 第一百零五节,JavaScript正则表达式
  7. 转:C++输入一行字符串的一点小结
  8. QPS/TPS简介
  9. Linux下的有用命令
  10. linux su失败:无法设置用户ID:资源暂时不可用
  11. C#装箱和拆箱。
  12. BZOJ 1443 游戏(二分图博弈)
  13. 查询sql数据库中表占用的空间大小
  14. 2018AVA: A Video Dataset of Spatio-temporally Localized Atomic Visual Actions
  15. 【Codeforces 142C】Help Caretaker
  16. ES6入门基础
  17. JSP与Servlet之间传值的一般方法,适合新手做练习
  18. fopen flock fclose 文件用法
  19. 1067 - Combinations---LightOj(Lucas求组合数)
  20. 更好的浏览器动画实现 requestAnimationFrame

热门文章

  1. JQuery EasyUI Tree组件的Bug记录
  2. 【C#常用方法】1.DataTable与List<T>的相互转换
  3. 对 GAN 的 value function 的理解
  4. tkinter的set()与get()
  5. OpenFire后台插件上传获取webshell及免密码登录linux服务器
  6. idea注释类,方法
  7. Windows Server 2012 R2 配置IIS
  8. IDEA编码时卡顿问题
  9. 启动keepalived报错(VI_1): received an invalid passwd!
  10. 开机没有deepin启动项的解决办法