首页 > Web开发 > 详细

js 文件下载 兼容ie

时间:2019-05-15 15:49:01      阅读:722      评论:0      收藏:0      [点我收藏+]

前置条件:后台接口返回二进制流文件

一、设置前端请求的的

  

responseType: ‘blob‘ 

 

二、接收请求数据并调用下载

var content = res.data // 接口返回的二进制流
var filename = fileName.xls // 文件名,根据需要更改
var blob = new Blob([content], {type: ‘application/vnd.ms-excel‘}) // 转化为blob对象 if (window.navigator.msSaveOrOpenBlob) { // IE navigator.msSaveBlob(blob, filename) } else { var aTag = document.createElement(‘a‘) aTag.download = filename aTag.href = URL.createObjectURL(blob) aTag.click() URL.revokeObjectURL(blob) }

  

var blob = new Blob([content], {type: ‘application/vnd.ms-excel‘})
if (window.navigator.msSaveOrOpenBlob) {
navigator.msSaveBlob(blob, filename)
} else {
var aTag = document.createElement(‘a‘)
aTag.download = filename
aTag.href = URL.createObjectURL(blob)
aTag.click()
URL.revokeObjectURL(blob)
}

js 文件下载 兼容ie

原文:https://www.cnblogs.com/baikouLoser/p/10869502.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!