阅读:51
# 这里返回一个迭代器,防止文件过大将内存打满,这样可以保证服务的占用内存几乎没有波动
def read_file(file_name, buf_size=409600):
with open(file_name, "rb") as f:
while True:
c = f.read(buf_size)
if c:
yield c
else:
break
@action(detail=False, methods=['POST'])
def export_templates(self, request, *args, **kwargs):
file_name = "取的名字根据项目上下文决定"
# 使用文件流的方式返回
res = StreamingHttpResponse(read_file(file_name))
# 增加headers
res['Content-Type'] = 'application/octet-stream'
res['Access-Control-Expose-Headers'] = "Content-Disposition, Content-Type"
res['Content-Disposition'] = "attachment;"
return res
这里对axios的封装就不说了
axios请求的参数: url,方法名,参数,responseType要带上:
url: ‘xxxx’,
method: ‘post’,
responseType: ‘blob’,
data
请求后返回的response处理:
.then(response => {
const filename = "xxx" + '.tar'
const blob = new Blob([response], {
type: 'application/x-tar'
})
// console.log("fileName",fileName)
if (window.navigator.msSaveOrOpenBlob) {
navigator.msSaveBlob(blob, filename)
} else {
var link = document.createElement('a')
link.href = window.URL.createObjectURL(blob)
link.download = filename
link.click()
// 释放内存
window.URL.revokeObjectURL(link.href)
}
})
tips:
参考自:https://blog.csdn.net/Zhuangvi/article/details/121063203
后缀名 | 文件类型 | 类型(type) |
---|---|---|
.xls | Microsoft Excel | application/vnd.ms-excel |
.xlsx | Microsoft Excel (OpenXML) | application/vnd.openxmlformats-officedocument.spreadsheetml.sheet |
.csv | CSV | text/csv |
.doc | Microsoft Word | application/msword |
.docx | Microsoft Word (OpenXML) | application/vnd.openxmlformats-officedocument.wordprocessingml.document |
application/pdf | ||
.ppt | Microsoft PowerPoint | application/vnd.ms-powerpoint |
.pptx | Microsoft PowerPoint (OpenXML) | application/vnd.openxmlformats-officedocument.presentationml.presentation |
.png | 便携式网络图形(PNG) | image/png |
.gif | GIF | image/gif |
.jpeg | JPEG 图片 | image/jpeg |
.jpg | JPEG 图片 | image/jpeg |
.mp3 | MP3 音频 | audio/mpeg |
.aac | AAC 音频 | audio/aac |
.html | 超文本标记语言 (HTML) | text/html |
.css | CSS | text/css |
.js | JavaScript | text/javascript |
.json | JSON 格式 | application/json |
.abw | AbiWord 文档 | application/x-abiword |
.arc | 存档文档(多个文件嵌入) | application/x-freearc |
.avi | AVI: 音频视频交错 | video/x-msvideo |
.azw | 亚马逊Kindle电子书格式 | application/vnd.amazon.ebook |
.bin | 任何类型的二进制数据 | application/octet-stream |
.bmp | Windows OS/2位图图形 | image/bmp |
.bz | BZip 存档 | application/x-bzip |
.bz2 | BZip2 存档 | application/x-bzip2 |
.csh | C-Shell 脚本 | application/x-csh |
.eot | MS嵌入式OpenType字体 | application/vnd.ms-fontobject |
.epub | 电子出版物(EPUB) | application/epub+zip |
.htm | 超文本标记语言 (HTML) | text/html |
.ico | Icon 格式 | image/vnd.microsoft.icon |
.ics | iCalendar 格式 | text/calendar |
.jar | Java Archive (JAR) | application/java-archive |
.jsonld | JSON-LD 格式 | application/ld+json |
.mid | 乐器数字接口(MIDI) | audio/midi audio/x-midi |
.midi | 乐器数字接口(MIDI) | audio/midi audio/x-midi |
.mjs | JavaScript 模块 | text/javascript |
.mpeg | MPEG 视频 | video/mpeg |
.mpkg | 苹果安装程序包 | application/vnd.apple.installer+xml |
.odp | OpenDocument演示文档 | application/vnd.oasis.opendocument.presentation |
.ods | OpenDocument 电子表格文件 | application/vnd.oasis.opendocument.spreadsheet |
.odt | OpenDocument 文本文档 | application/vnd.oasis.opendocument.text |
.oga | OGG 音频 | audio/ogg |
.ogv | OGG 视频 | video/ogg |
.ogx | OGG | application/ogg |
.otf | OpenType 字体 | font/otf |
.rar | RAR 存档 | application/x-rar-compressed |
.rtf | 富文本格式 (RTF) | application/rtf |
.sh | Bourne shell 脚本 | application/x-sh |
.svg | 可缩放矢量图形 (SVG) | image/svg+xml |
.tar | Tape 归档(TAR) | application/x-tar |
.tif | 标记图像文件格式 (TIFF) | image/tiff |
.tiff | Tagged Image File Format (TIFF) | image/tiff |
.ttf | TrueType 字体 | font/ttf |
.txt | Text | text/plain |
.vsd | Microsoft Visio | application/vnd.visio |
.wav | 波形音频格式 | audio/wav |
.weba | WEBM 音频 | audio/webm |
.webm | WEBM 视频 | video/webm |
.webp | WEBP 图片 | image/webp |
.xhtml | XHTML | application/xhtml+xml |
.xml | XML | application/xml(普通用户不可读)、text/xml(普通用户可读) |
.xul | XUL | application/vnd.mozilla.xul+xml |
.zip | ZIP | application/zip |
.3gp | 3GPP | audio/video 容器 video/3gpp、audio/3gpp(不含视频) |
.3g2 | 3GPP2 | audio/video 容器 video/3gpp2、audio/3gpp2(不含视频) |
.7z | 7-zip | application/x-7z-compressed |
.swf | 小型web格式 (SWF) or Adobe Flash document | application/x-shockwave-flash |
.woff | 网页开放字体格式 (WOFF) | font/woff |
.woff2 | 网页开放字体格式 (WOFF) | font/woff2 |