下载PDF文件不适用于angular和JavaScript

下载PDF文件不适用于angular和JavaScript,javascript,angularjs,blob,Javascript,Angularjs,Blob,我使用下面的代码下载PDF文件。文件已下载,但不包含任何数据 var blob = new Blob([response], { type: "text/plain"}); var url = window.URL.createObjectURL(blob); var link = document.createElement('a'); link.href = url; link.download = e.coAttachmentName;

我使用下面的代码下载PDF文件。文件已下载,但不包含任何数据

     var blob = new Blob([response], { type: "text/plain"});
     var url = window.URL.createObjectURL(blob); 
     var link = document.createElement('a');
     link.href = url;
     link.download = e.coAttachmentName; 
     link.click();

使用FileSaver.js,从这里获取它


saveAs将为您下载一个pdf文件。

首先,您的请求-响应类型必须是
arraybuffer

function openFile(response,fileName,saveFile){
  var fileURL;
  var contentType = response.headers('Content-Type');
  var blob = new $window.Blob([response.data], { type: contentType });
  if(saveFile){
      saveAs(blob, fileName);
  }else{
    if($window.navigator.msSaveOrOpenBlob){
        $window.navigator.msSaveOrOpenBlob(blob , fileName);
    }else{
        fileURL = URL.createObjectURL(blob);
        $window.open(fileURL, '_blank');
    }
  }
}
$window
是一个AngularJS服务-对browers
window
对象的引用

$window.navigator.msSaveOrOpenBlob
-这是一种特定于IE浏览器的方法,支持创建和下载文件,而不是IE不支持的Blob URL

saveAs
-

此参考资料将帮助您:
function openFile(response,fileName,saveFile){
  var fileURL;
  var contentType = response.headers('Content-Type');
  var blob = new $window.Blob([response.data], { type: contentType });
  if(saveFile){
      saveAs(blob, fileName);
  }else{
    if($window.navigator.msSaveOrOpenBlob){
        $window.navigator.msSaveOrOpenBlob(blob , fileName);
    }else{
        fileURL = URL.createObjectURL(blob);
        $window.open(fileURL, '_blank');
    }
  }
}