admin管理员组

文章数量:1650146

已知文件URL;前端实现下载

为了防止打开是预览;采用这样方法下载;直接调用即可;


const downUrl = (fileName, url) => {
  let x = new XMLHttpRequest();
  x.open("GET", url, true);
  x.responseType = "blob";
  x.onload = function(e) {
    let blob = x.response;
    if ("msSaveOrOpenBlob" in navigator) {
      //IE导出
      window.navigator.msSaveOrOpenBlob(blob, fileName);
    } else {
      let a = document.createElement("a");
      a.download = fileName;
      a.href = URL.createObjectURL(blob);
      document.querySelector("body").appendChild(a);
      a.click();
      document.querySelector("body").removeChild(a);
    }
  };
  x.send();
};

本文标签: 文件浏览器urljs