分享

js 实现文件下载/文件导出。

 怡红公子0526 2021-09-27

1. POST方式进行文件导出;

  // url 下载URL
  // fileName 下载文件名称
  function exportFile(url, fileName) {
    let xhr = new XMLHttpRequest();
    xhr.open("POST", url);
    xhr.responseType = "blob";
    xhr.onload = () => {
      let ctx = xhr.response;
      let blob = new Blob([ctx]);      if ("msSaveOrOpenBlob" in navigator) {//兼容IE        window.navigator.msSaveOrOpenBlob(blob, fileName);
      } else {
        let aLink = document.createElement("a");
        aLink.download = fileName;
        aLink.style.display = "none";
        aLink.href = URL.createObjectURL(blob);
        document.body.appendChild(aLink);
        aLink.click();
        document.body.removeChild(aLink);
      }
    };
    xhr.send();
  }

使用方法:exportFile(url,fileName);

使用Angular方式进行导出:

.http.request("POST",url,{},{responseType:"blob"=>=  ("msSaveOrOpenBlob"  navigator) {= document.createElement("a"== "none"==>= = (e)=>(e && e["target"= JSON.parse(e["target"]["result"(errorMsg && errorMsg["code""有报错,出错了。。。。。"

2. GET方式进行文件导出;

// url 下载路径window.location = url;

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多