简体   繁体   English

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

[英]Download PDF file is not working with angular and JavaScript

在此处输入图片说明

I am using below code for downloading PDF files.File is downloaded but doesn't contain any data. 我正在使用以下代码下载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();

Use FileSaver.js , get it from here https://github.com/eligrey/FileSaver.js/ 使用FileSaver.js ,从这里获取它https://github.com/eligrey/FileSaver.js/

var filename = new Date();
var blob = new Blob([response], {type: "application/pdf;charset=utf-8"});
saveAs(blob, filename +".pdf");

saveAs will download a pdf file for you. saveAs将为您下载一个pdf文件。

First of all your request response type has to be arraybuffer . 首先,您的请求响应类型必须为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 is an AngularJS service - reference to browers window object. $window是AngularJS服务-对浏览器window对象的引用。

$window.navigator.msSaveOrOpenBlob - it's a method specific to IE browser that supports creating and downloading files insted of Blob URL which isn't supported by IE. $window.navigator.msSaveOrOpenBlob这是一种特定于IE浏览器的方法,它支持创建和下载IE不支持的由Blob URL插入的文件。

saveAs - https://github.com/eligrey/FileSaver.js/ saveAs - https://github.com/eligrey/FileSaver.js/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM