IE兼容 下载

IE不支持a标签的download
可以使用IE自带的msSaveBlob方法

const fileNameEncode =
  res.headers["content-disposition"].split("filename=")[1];
// 文件名 解码
const fileName = decodeURI(fileNameEncode);
console.log("fileName", fileName);
let blob = new Blob([res.data], {
  type: "application/msword,charset=utf-8",
});
const blobUrl = window.URL.createObjectURL(blob);
if (window.navigator.msSaveBlob) {
  //判断了有该方法即为IE浏览器
  try {
    window.navigator.msSaveBlob(blob, fileName);
  } catch (e) {
    console.log(e);
  }
} else {
  let link = document.createElement("a");
  link.style.display = "none";
  link.href = blobUrl;
  link.download = fileName;
  document.body.appendChild(link);
  link.click();
}
上一篇:前端小技巧:JavaScript 编码规范


下一篇:使用useEffect 构建加载进度条与报错信息