金狮镖局 Design By www.egabc.com
1 StreamingHttpResponse下载
StreamingHttpResponse(streaming_content):流式相应,内容的迭代器形式,以内容流的方式响应。
注:StreamingHttpResponse一般多现实在页面上,不提供下载。
以下为示例代码
def streamDownload(resquest): def file_iterator(filepath, chunk_size = 512): with open(filepath, 'rb') as f: while True: con = f.read(512) if con: yield con else: break filename = os.path.abspath(__file__) + 'test.txt' response = StreamingHttpResponse(file_iterator(filename) return response # 最后程序会将结果打印在显示器上
2 FileResponse下载
FileResponse(stream):以流形式打开后的文件
注:FileResponse是StreamingHttpResponse的子类
以下为示例代码:
def homeproc2(request): cwd = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) response = FileResponse(open(cwd + "/msgapp/templates/youfile", "rb")) response['Content-Type] = 'application/octet-stream' response['Content-Disposition'] = 'attachment;filename="filename"' return response
需要解释说明的是:
response['Content-Type] = 'application/octet-stream' response['COntent-Disposition'] = 'attachment;filename="filename"'
- Content-Type:用于指定文件类型。
- COntent-Disposition:用于指定下载文件的默认名称,对,没错! “CO”两个字符都要大写。
两者都是MIME协议里面的标准类型。
金狮镖局 Design By www.egabc.com
金狮镖局
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件!
如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
金狮镖局 Design By www.egabc.com
暂无详解Django关于StreamingHttpResponse与FileResponse文件下载的最优方法的评论...
更新日志
2024年09月23日
2024年09月23日
- 银霞.1983-《你那好冷的小手》珍藏纪念版[WAV分轨]
- 银霞.2002-《国语巨星专辑VOL.3》2CD台湾版[WAV+CUE]
- 金池刘罡《昨日情歌HQ》头版限量编号[低速原抓WAV+CUE]
- 段奥娟.2024-23·岁碎念【白米范】【FLAC分轨】
- 顺子.2001-昨日·唯一·更多·顺子自选辑【魔岩】【WAV+CUE】
- 无印良品.1999-私藏三首·原创相对论(EP)【滚石】【WAV+CUE】
- Merryland《Merryland》[FLAC/分轨][170.22MB]
- 庄锭欣 ELKIE《没有你的未来》[320K/MP3][15.78MB]
- 庄锭欣 ELKIE《没有你的未来》[FLAC/分轨][36MB]
- TchaikovskySwanLake,Op.20柴可夫斯基天鹅湖(2013)SACD[Hi-Res]
- 24bit达人艺典《琴歌终极版》整轨WAV+CUE
- 黑鸭子.2005-《发烧DSD》柏菲[WAV分轨]
- 群星.1990-电影看板·人间道【宝丽金】【WAV+CUE】
- 周殷廷.2024-TO.WHOM.IT.MAY.CONCERN【环球】【FLAC分轨】
- 草蜢.1997-三人世界精选17首【宝丽金】【WAV+CUE】