当前位置: 移动技术网 > 科技>操作系统>windows > SCRIPT5: 拒绝访问。Excel文件在IE下导不出来

SCRIPT5: 拒绝访问。Excel文件在IE下导不出来

2020年09月22日  | 移动技术网科技  | 我要评论
文件在IE下导不出来原来是由于在IE中不支持手动调用click()方法,所以在下载Excel文件的时候增加判断:if (navigator.msSaveOrOpenBlob) {//允许用户在客户端上保存文件,IE //IE浏览器 navigator.msSaveOrOpenBlob(blobData,fileName);//blobData为下载文件流,fileName为下载文件名 } else { //其他浏览器 let url = window.URL.cre

文件在IE下导不出来
在这里插入图片描述原来是由于在IE中不支持手动调用click()方法,所以在下载Excel文件的时候增加判断:

if (navigator.msSaveOrOpenBlob) {//允许用户在客户端上保存文件,IE
 	 //IE浏览器
     navigator.msSaveOrOpenBlob(blobData,fileName);//blobData为下载文件流,fileName为下载文件名
 } else {
     //其他浏览器
     let url = window.URL.createObjectURL(blobData);
     console.log('url',url);
     let link = document.createElement('a');
     link.style.display = 'none';
     link.href = url;
     link.setAttribute('download', fileName);
     document.body.appendChild(link);
     link.click();//IE下不支持该方法
     document.body.removeChild(link);
 }

完整Excel文件下载方法如下:

handleDownload(){
    let fileName='222';
    //fileName=encodeURIComponent(fileName);
    console.log(fileName);
    //导出
    if(this.ID){
      this.toDownload(this.uploadUrl,this.ID,fileName+'.xlsx')
    }else{
        this.$message.error('没有数据,无法导出!');
    }
},
toDownload(url, data, fileName) {
    return new Promise((resolve, reject) => {
        axios({
            method: "get",
            url: url,//请求后端接口url
            params:{ChlinicalID:data},
            responseType: 'blob'
        })
        .then(res => {
            let reader = new FileReader();
            let data = res.data;
            console.log(res);
            reader.onload = e => {
                if (e.target.result.indexOf('Result') != -1 && JSON.parse(e.target.result).Result == false) {
                    // 进行错误处理
                } else {
                    if (!fileName) {
                        let contentDisposition = res.headers['content-disposition'];
                        if (contentDisposition) {
                            fileName = window.decodeURI(res.headers['content-disposition'].split('=')[2].split("''")[1], "UTF-8");
                        }
                    }
                    this.executeDownload(data, fileName);
                }
            };
            reader.readAsText(data);
            resolve(res.data);
        })
    });
},
 //  模拟点击a 标签进行下载
executeDownload(data, fileName) {
  if (!data) {
         return
     }
     var blobData=new Blob([data]);
     if (navigator.msSaveOrOpenBlob) {//允许用户在客户端上保存文件,IE
         //IE浏览器
         navigator.msSaveOrOpenBlob(blobData,fileName);
     } else {
         //其他浏览器
         let url = window.URL.createObjectURL(blobData);
         console.log('url',url);
         let link = document.createElement('a');
         link.style.display = 'none';
         link.href = url;
         link.setAttribute('download', fileName);
         document.body.appendChild(link);
         link.click();//IE下不支持该方法
         document.body.removeChild(link);
     }
 },

本文地址:https://blog.csdn.net/Mayflyingdancing/article/details/108725142

如您对本文有疑问或者有任何想说的,请点击进行留言回复,万千网友为您解惑!

相关文章:

验证码:
移动技术网