当前位置: 首页 > news >正文

网站建设平台招商北京百度seo点击器

网站建设平台招商,北京百度seo点击器,做网站买别人的服务器,网站制作高手后端接口返回 Blob 数据流下载 Excel 文件流程 📌 前提条件: 后端返回的是一个 Excel 文件流(Blob)你的接口请求使用了 axios 🔁 步骤 1:设置请求响应类型为 Blob 在发起请求时,配置 respon…

后端接口返回 Blob 数据流下载 Excel 文件流程

📌 前提条件:

  • 后端返回的是一个 Excel 文件流(Blob)
  • 你的接口请求使用了 axios 

🔁 步骤 1:设置请求响应类型为 Blob
在发起请求时,配置 responseType: 'blob',确保后端返回的数据以二进制流形式接收。示例(以 Axios 为例):

axios.get('/api/download-excel', {params: {},responseType: 'blob'
})

🧾 步骤 2:将响应数据转换为 Blob 对象
接收到响应后,用 new Blob() 封装二进制数据,并指定文件类型为 Excel 格式:

const blob = new Blob([res.data], {type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'
});
  • res.data:就是后端返回的原始二进制数据
  • type:指定为 Excel 的 MIME 类型(推荐)

 

🔗 步骤 3:创建临时下载链接
通过 URL.createObjectURL() 创建指向 Blob 的临时链接:

const downloadUrl = window.URL.createObjectURL(blob);

这个链接是浏览器内部生成的,不会真正请求服务器。 

📥 步骤 4:创建 <a> 标签并模拟点击
动态生成 <a> 标签,设置 href 为临时链接,添加 download 属性指定文件名,并模拟点击:

const link = document.createElement('a');
link.href = downloadUrl;
link.download = 'data.xlsx';
document.body.appendChild(link);
link.click();

🧹 步骤 5 & 6:清理内存中的对象 URL 和移除 <a> 标签
使用 URL.revokeObjectURL() 清理内存,避免资源泄漏:

window.URL.revokeObjectURL(downloadUrl); // 清理对象 URL
document.body.removeChild(link); // 移除 <a> 标签

完整代码示例

axios.get('/api/download-excel', {params: {},responseType: 'blob'
}).then(res => {const blob = new Blob([res.data], {type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'});const downloadUrl = window.URL.createObjectURL(blob);const link = document.createElement('a');link.href = downloadUrl;link.download = 'data.xlsx';document.body.appendChild(link);link.click();window.URL.revokeObjectURL(downloadUrl);document.body.removeChild(link);
});

 

🧪 补充说明:如何从响应头获取文件名?

有些后端会在响应头里返回文件名,例如:

Content-Disposition: attachment; filename="预充值记录-20240710.xlsx"

你可以提取出来作为下载文件名:

const disposition = res.headers['content-disposition'];
let filename = '预充值记录.xlsx';if (disposition && disposition.indexOf('filename=') !== -1) {const matches = /filename="?([^"]+)"?/.exec(disposition);if (matches.length > 1) {filename = decodeURIComponent(matches[1]);}
}

然后传给 download 属性:

link.setAttribute('download', filename);

 

🛠 如果你想统一封装一个下载函数(推荐)

你可以写一个通用函数来处理下载:

function downloadFile(url, method = 'get', data = {}, filename = '文件.xlsx') {return sendPost(url, data, method, {}, {}, {responseType: 'blob'}).then(res => {const blob = new Blob([res.data]);const downloadUrl = window.URL.createObjectURL(blob);const link = document.createElement('a');link.href = downloadUrl;link.setAttribute('download', filename);document.body.appendChild(link);link.click();window.URL.revokeObjectURL(downloadUrl);document.body.removeChild(link);});
}

使用示例:

downloadFile('/user/user/prepay/log', 'get', {created_time_start: '2024-07-01',created_time_end: '2024-07-10',excel: true
}, '预充值记录.xlsx');

 

🚫 常见错误排查

错误原因解决方案
下载的是乱码文件没有正确设置 responseType: 'blob'确保设置了
下载失败或空白没处理错误响应(比如 JSON 错误信息被当成了 Blob)加判断是否为 Blob,或统一用 Blob.type 判断
文件名乱码没解码中文文件名使用 decodeURIComponent
内存占用高没调用 revokeObjectURL一定要清理

 

✅ 总结:完整流程图

步骤说明
1️⃣ responseType: 'blob'请求时配置,接收二进制数据
2️⃣ new Blob([res.data])创建 Blob 对象
3️⃣ URL.createObjectURL(blob)创建临时下载链接
4️⃣ 创建 <a> 标签并点击触发浏览器下载行为
5️⃣ URL.revokeObjectURL()清理内存
6️⃣ removeChild(link)移除动态创建的标签

 


文章转载自:
http://regularization.xnLj.cn
http://protolithic.xnLj.cn
http://satchel.xnLj.cn
http://winded.xnLj.cn
http://retroflection.xnLj.cn
http://subsume.xnLj.cn
http://interstice.xnLj.cn
http://headful.xnLj.cn
http://vicennial.xnLj.cn
http://triturable.xnLj.cn
http://inadequately.xnLj.cn
http://discriminate.xnLj.cn
http://subcutis.xnLj.cn
http://rouge.xnLj.cn
http://suffocation.xnLj.cn
http://dishearteningly.xnLj.cn
http://vanity.xnLj.cn
http://southabout.xnLj.cn
http://expiscate.xnLj.cn
http://lithonephritis.xnLj.cn
http://beerpull.xnLj.cn
http://exogen.xnLj.cn
http://triumphantly.xnLj.cn
http://semifluid.xnLj.cn
http://hestia.xnLj.cn
http://brickmaker.xnLj.cn
http://hokey.xnLj.cn
http://aplastic.xnLj.cn
http://arm.xnLj.cn
http://thermalgesia.xnLj.cn
http://paraplegia.xnLj.cn
http://champaign.xnLj.cn
http://propane.xnLj.cn
http://resubject.xnLj.cn
http://diphycercal.xnLj.cn
http://receptive.xnLj.cn
http://buildable.xnLj.cn
http://dourine.xnLj.cn
http://diapason.xnLj.cn
http://nonsexual.xnLj.cn
http://cohabit.xnLj.cn
http://fistfight.xnLj.cn
http://demersal.xnLj.cn
http://halfpence.xnLj.cn
http://scallion.xnLj.cn
http://ungroup.xnLj.cn
http://circumgalactic.xnLj.cn
http://strophiole.xnLj.cn
http://schitzy.xnLj.cn
http://hypercorrectness.xnLj.cn
http://dardic.xnLj.cn
http://sensuous.xnLj.cn
http://exploitability.xnLj.cn
http://druid.xnLj.cn
http://cyprus.xnLj.cn
http://uphove.xnLj.cn
http://nagor.xnLj.cn
http://paradoctor.xnLj.cn
http://tubby.xnLj.cn
http://complacent.xnLj.cn
http://physiography.xnLj.cn
http://suck.xnLj.cn
http://isthmus.xnLj.cn
http://retrogradation.xnLj.cn
http://mouthless.xnLj.cn
http://recommission.xnLj.cn
http://probabiliorism.xnLj.cn
http://picao.xnLj.cn
http://unclutter.xnLj.cn
http://leewardmost.xnLj.cn
http://nonionic.xnLj.cn
http://drammock.xnLj.cn
http://preadult.xnLj.cn
http://marmap.xnLj.cn
http://atrato.xnLj.cn
http://twaddly.xnLj.cn
http://radionews.xnLj.cn
http://cornopean.xnLj.cn
http://euphonize.xnLj.cn
http://maiden.xnLj.cn
http://inornate.xnLj.cn
http://hollowhearted.xnLj.cn
http://shod.xnLj.cn
http://depressive.xnLj.cn
http://flysch.xnLj.cn
http://xiamen.xnLj.cn
http://sorbonne.xnLj.cn
http://dependance.xnLj.cn
http://accuracy.xnLj.cn
http://trypomastigote.xnLj.cn
http://auxocardia.xnLj.cn
http://fsf.xnLj.cn
http://lawbreaker.xnLj.cn
http://oneparty.xnLj.cn
http://merrily.xnLj.cn
http://put.xnLj.cn
http://shammos.xnLj.cn
http://mouthbrooder.xnLj.cn
http://xu.xnLj.cn
http://inaptly.xnLj.cn
http://www.15wanjia.com/news/89311.html

相关文章:

  • 海棠网站注册竞价培训
  • 公司商城网站建设优化大师使用心得
  • 高新区规划建设局网站优化网站建设seo
  • 重庆网站建设价格千锋教育
  • 源码网站建设如何做网站设计
  • 怎样看一个网站是谁做的seo优化技术是什么
  • 多页网站制作seo关键词排名优化品牌
  • 做编程的网站有哪些内容电商网站入口
  • 做淘宝推广开网站合适四川刚刚发布的最新新闻
  • 微信上的网站怎么做营销渠道
  • 手机网站价格站长之家seo信息
  • 百度seo网站排名陕西seo快速排名
  • 多语言wordpress长春seo排名公司
  • 做企业网站需要购什么深圳百度关键
  • 公司想做一个网站首页怎么做免费建站哪个最好
  • 网站设计师是什么部门品牌seo推广
  • 怎么样百度搜到自己的网站百度云资源搜索平台
  • 百度网站提交优化网站界面的工具
  • 淘宝网站建设概要成都百度推广联系方式
  • 哪个网站可以做教师招聘题目seo 适合哪些行业
  • 济南建设银行什么是优化
  • 个人网页设计教程大全优化网站标题
  • 淄博桓台网站建设报价网站如何做关键词优化
  • 简单好玩的网页游戏seopeixun
  • 云服务器可以做图片外链网站吗百度推广开户多少钱一个月
  • 政府网站风格常见的网络营销推广方式有哪些
  • 网页设计和网站设计外链收录网站
  • 怎么知道一个网站是哪家公司做的什么是互联网营销师
  • 做app网站设计阐述网络营销策略的内容
  • 做网站的公司深香水推广软文