admin管理员组

文章数量:1533913

近期运维人员提出一个bug,页面中的文件在IE浏览器不可以下载,但是在火狐和谷歌浏览器是正常下载的。

控制台发现ie下载时报错:Unhandled promise rejection Error: 拒绝访问。刚开始以为是promise问题,后来发现是IE对于base64流数据有独特的处理方式。添加下方标红代码块后问题解决。

<a style={{ marginRight: 10 }} onClick={() => {

    new Promise((resolve, reject) => {
        axios({
            method: 'post',
            url: 'xxxxxxxxxxxxxxxxxxxxx',

            headers: {
                Authorization: JSON.parse(sessionStorage.getItem("xxxxxxxx")).value
            },
            responseType: 'blob'
        }).then(res => {
            resolve(res);
        }).catch(err => {
            reject(err);
        });
    }).then((res) => {
        // 判断当前是否为IE浏览器
        if(!!window.ActiveXObject || "ActiveXObject" in window){
            // console.log("是ie浏览器,下载做特殊处理");
            // 保存或打开两个选项
            window.navigator.msSaveOrOpenBlob(res.data,record.originalName);
            // 只有保存选项
            window.navigator.msSaveBlob(res.data,record.originalName); 
        } else {
            // console.log("非ie浏览器");
            downloadFile(res.data, record.originalName, record.fileSuffix)
        }
    })

}}>下载3</a>

本文标签: 浏览器数据