本文主要是介绍nest流式文件下载,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
后端:
import { Response } from 'express';
import { join } from "path"
import { zip } from "compressing"//流式下载@Get('stream')async down(@Res() res: Response) {const url = join(__dirname, "../images/b10ab40e11038b5b939dea024576131b43.jpg")const tarStream = new zip.Stream()await tarStream.addEntry(url)//需要设置请求头res.setHeader("Content-Type", "application/octet-stream")res.setHeader("Content-Disposition", "attachment; filename=123.jpg")tarStream.pipe(res)}
前端vue
const downloadfile = async (url: string) => {const res = await fetch(url).then((res) => {console.log(res)return res.arrayBuffer();})console.log([res])const blob = new Blob([res])console.log(blob)const urls = URL.createObjectURL(blob);console.log(urls)const a = document.createElement('a')a.href = urlsa.download = 'xiaoming.zip'a.click()
}
这篇关于nest流式文件下载的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!