一.转换
//将blob转换为urlconst changeBlobToUrl = blobData => {return new Promise(resolve => {//创建Blob对象const blob = new Blob([blobData])// 创建FileReader对象const reader = new FileReader()reader.onload = function (e) {resolve(e.target.result)}// 使用FileReader读取Blob数据reader.readAsDataURL(blob)})}
二.下载
//前端下载blob的url类型文件const downloadFileByBlobUrl = (url: string, name: string) => {aDownload(url, name)URL.revokeObjectURL(url)}const aDownload = (url, name) => {// 创建<a>元素const link = document.createElement("a")// 设置<a>元素的属性link.href = urllink.download = name// 将<a>元素添加到文档中document.body.appendChild(link)// 触发点击事件link.click()// 移除<a>元素document.body.removeChild(link)}