如何接收二进制PDF的HTTP响应并显示它. 角度 2 - 离子2.



我正在尝试从休息服务接收一个 pdf 转换为二进制 (64(,并在 ionic 2 中显示它,我试试这个:

我正在从休息服务中接收一个转换为二进制(64(的pdf,并在ionic 2中显示它,我试试这个:

    in service
    function(token:String, docCaseNumber: String){
      this.setTokenHeaders(token);
      this.headers.append('Accept', 'application/octet-stream');      
      let options = new RequestOptions({ headers: this.headers });     
      return new Promise(resolve => {
        this.http.get(url, options)
        .map(res => res)
        .subscribe(data => {
          //console.log(data);
            resolve(data.text());
          }, err => {
            resolve("connException");
          });
        }); 
    }
       in component
     getXX(){
      this.miservice.functoin(this.token, 'H17-09601').then(
          (data) => {
              var blob = data;
              console.log(blob);
              this.base64ToUint8Array(data);
              //this.getBase64(data);
              var converted = new Blob([blob], {type:'application/pdf'});
              this.convertToBase64(converted);
              //this.base64ToUint8Array(converted);
              //this.fileURL = URL.createObjectURL(converted);
              //window.open(fileURL);
              var pdf = pdfMake.createPdf(this.buildPdf(converted));
              pdf.getBase64(function (output) {
                this.base64ToUint8Array(output);
              });
              //console.log(pdf);
              pdf.open;
              //console.log(data);
    });
  }

这是答案。你可以用它来显示pdf 根据您的需要修改;)

 return new Promise(resolve => {
            let xhr = new XMLHttpRequest();
            xhr.open('get', url, true);
            xhr.setRequestHeader("Access-Control-Allow-Origin", '*');
            xhr.setRequestHeader('Accept', 'application/octet-stream');
            xhr.setRequestHeader('Authorization', 'Bearer ' + token);
            // xhr.overrideMimeType('text/xml; charset=iso-8859-1');
            xhr.overrideMimeType('text/xml; charset=iso-8859-1');
            xhr.responseType = "arraybuffer";
            xhr.onreadystatechange= function () {
                if (xhr.readyState === 4) {
                    if (xhr.status === 200) {
                        console.log(JSON.stringify((xhr.response)));
                        resolve(new Blob([xhr.response], { type: 'application/pdf' }));
                    } else {
                        //do something
                    }
                }
            }
            xhr.send();
            }); 
then use it like this:
    your-function.then(
              (blob :Blob) => {
                var downloadUrl = URL.createObjectURL(blob);
                var downloadLink      = document.createElement('a');
                downloadLink.href = downloadUrl;
                downloadLink.target   = '_blank';
                document.body.appendChild(downloadLink);
                downloadLink.click();
              });

相关内容

  • 没有找到相关文章

最新更新