axios({ method: 'post', url: 'url....', data:{}, timeout: 1000*60, responseType: 'blob',// 此选项必须设置 否则保存后的文件无法播放 headers: { 'X-Requested-With': 'XMLHttpRequest', 'Content-Type': 'application/json; charset=UTF-8', } }).then((response) => { if (response && (response.status === 200 || response.status === 304 || response.status === 400)) { var fileName = response.headers['content-disposition'].split(';')[1].split('=')[1]; var contentType = response.headers['content-type']; var blob = new Blob([response.data])
//如果是IE 通过msSaveBlob 下载 ,否则走h5下载 if(navigator.msSaveBlob){ navigator.msSaveBlob(blob, fileName); }else{ var a = document.createElement('a'); a.download = fileName; a.innerHTML = 'download'; a.href = URL.createObjectURL(blob);
//自定义事件触发 var event = document.createEvent('MouseEvents'); event.initMouseEvent('click', true, false, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null); a.dispatchEvent(event); URL.revokeObjectURL(a.href) // 释放URL 对象 } return '下载成功!'; } else { return '网络异常'; } }).catch(error=>{
return '网络异常';
})