js 文件下载 兼容ie
前置条件:后台接口返回二进制流文件
一、设置前端请求的的
SRE实战 互联网时代守护先锋,助力企业售后服务体系运筹帷幄!一键直达领取阿里云限量特价优惠。
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) }

更多精彩