django 下载文件,指定文件中文名称
2024-08-24 16:12:03
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
最新文章
- 使用OWIN作为WebAPI的宿主
- BZOJ 4582: [Usaco2016 Open]Diamond Collector
- 使用dd工具对磁盘RAID5和10进行I/O性能测试
- (转)浅谈ASP.NET报表控件
- DbUtility Ex
- 第一百零五节,JavaScript正则表达式
- 转:C++输入一行字符串的一点小结
- QPS/TPS简介
- Linux下的有用命令
- linux su失败:无法设置用户ID:资源暂时不可用
- C#装箱和拆箱。
- BZOJ 1443 游戏(二分图博弈)
- 查询sql数据库中表占用的空间大小
- 2018AVA: A Video Dataset of Spatio-temporally Localized Atomic Visual Actions
- 【Codeforces 142C】Help Caretaker
- ES6入门基础
- JSP与Servlet之间传值的一般方法,适合新手做练习
- fopen flock fclose 文件用法
- 1067 - Combinations---LightOj(Lucas求组合数)
- 更好的浏览器动画实现 requestAnimationFrame
热门文章
- JQuery EasyUI Tree组件的Bug记录
- 【C#常用方法】1.DataTable与List<;T>;的相互转换
- 对 GAN 的 value function 的理解
- tkinter的set()与get()
- OpenFire后台插件上传获取webshell及免密码登录linux服务器
- idea注释类,方法
- Windows Server 2012 R2 配置IIS
- IDEA编码时卡顿问题
- 启动keepalived报错(VI_1): received an invalid passwd!
- 开机没有deepin启动项的解决办法