其他分享
首页 > 其他分享> > js 实现下载本地文件

js 实现下载本地文件

作者:互联网

js下载本地文件

 //下载本地资源文件
    downPager(url, filename) {
      // return fetch(url).then(res => res.blob().then(blob => {
      //   let a = document.createElement('a');
      //   let url = window.URL.createObjectURL(blob);
      //   a.href = url;
      //   a.download = filename;
      //   a.click();
      //   window.URL.revokeObjectURL(url);
      // }))
      // var id = getSelectedRow();
      // if(id == null){
      //   return ;
      // }

      // var rowData = $("#jqGrid").getRowData(id);
      // var url = rowData.url;
      //window.location.href = "http://118.89.217.177:8096/resources/web-portal/paper.pdf" ;

      var xhr = new XMLHttpRequest();
      xhr.open('GET', "//xxx.pdf", true);
      xhr.responseType = "blob";//web-portal
      xhr.onreadystatechange = function () {
        if (xhr.readyState == 4) {
          if (xhr.status == 200) {
            var blod = this.response;
            console.log(blod)
            // var src = URL.createObjectURL(blod);
            var eleLink = document.createElement("a");
            eleLink.download = name || new Date().getTime() + ".pdf";
            eleLink.href = URL.createObjectURL(blod);
            document.body.appendChild(eleLink);
            eleLink.click();
          }
        }
      };
      xhr.send()


    },

 

标签:url,js,xhr,blod,blob,本地,var,eleLink,下载
来源: https://www.cnblogs.com/shuihanxiao/p/15118893.html