HLJ 发布于
2022-01-19 15:41:12

原生Ajax请求pdf数据流内容并下载代码

DownloadFile(fileName) {
  //Set the File URL.
  var url = "static/http://localhost:8080/static/test.pdf";
  //Create XMLHTTP Request.
  var req = new XMLHttpRequest();
  req.open("GET", url, true);
  req.responseType = "blob";
  req.onload = function () {
    //Convert the Byte Data to BLOB object.
    var blob = new Blob([req.response], { type: "application/octetstream" });

    //Check the Browser type and download the File.
    var isIE = false || !!document.documentMode;
    if (isIE) {
        window.navigator.msSaveBlob(blob, fileName);
    } else {
      var url = window.URL || window.webkitURL;
      var link = url.createObjectURL(blob);
      var a = document.createElement("a");
      a.setAttribute("download", fileName);
      a.setAttribute("href", link);
      document.body.appendChild(a);
      a.click();
      document.body.removeChild(a);
    }
  };
  req.send();
}
当前文章内容为原创转载请注明出处:http://www.good1230.com/detail/2022-01-19/573.html
最后生成于 2022-10-07 22:27:54