【问题标题】:Read PDF data from post从帖子中读取 PDF 数据
【发布时间】:2020-10-20 09:34:06
【问题描述】:

我正在努力从 post api 读取 PDF 数据(不是 pdf url),并使用我的 IONIC 4 android 应用程序中安装在手机上的 pdf 查看器打开 pdf。我的文件传输插件在下载该文件时给出错误代码 2。这是我编写的代码。我可以知道我哪里出错了吗?

my.ts 文件

  var apiData="https://kairavforex.com/media/documents/reports/hello_TYUtZF5.pdf";
        var sdsn={
           path:apiData
        }

   this.http.post('https://kairavforex.com/api/download_report/',sdsn,{'Content-Type': 'application/json','Authorization': "Token" + " " +  this.authToken})
        .then(data=>{
              console.log(data.data)              
              const fileTransfer: FileTransferObject = this.transfer.create();
              fileTransfer.download(data.data, this.file.externalDataDirectory + 'file.pdf').then((entry) => {
                this.fileOpener.showOpenWithDialog(entry.toURL(), 'application/pdf')
                .then(() => console.log('File is opened'))
                .catch(e => console.log('Error opening file', e));
              }, (error) => {
                 console.log(error)
              });
        })

这是错误的图像和我的 pdf 响应,这是正确的,因为当我将其保存到我的电脑 aspdf 并打开它时工作正常

控制台错误

【问题讨论】:

    标签: angular ionic-framework ionic4 angular8


    【解决方案1】:

    请按以下步骤操作,希望对您有所帮助

    第 1 步: 从https://ionicframework.com/docs/native/pdf-generator安装PDF生成器

    第2步:安装文件打开器以打开文件https://ionicframework.com/docs/native/file-opener

    第3步:安装文档查看器以查看文档https://ionicframework.com/docs/native/document-viewer

    【讨论】:

      【解决方案2】:

      通过使用 Files 和 fileOpener 插件解决了这个问题。从 api 响应中获取 pdf 数据后,我将其写入并保存到文件中,然后使用解决问题的 fileOpener 插件读取数据。

      this.urldatanam="hello.pdf";
        this.http.post('https://kaiabc.com/api/download_report/',downloadURl,{'Content-Type': 'application/json','Authorization': "Token" + " " +  this.authToken})
              .then(data=>{
                console.log(data)
                    var pdfread=data.data;
                    this.file.writeFile(this.file.externalDataDirectory, this.urldatanam, pdfread, { replace: true }).then(data=>{
                      console.log(data)
                      this.fileOpener.showOpenWithDialog(this.file.externalDataDirectory+this.urldatanam, 'application/pdf')
                      .then(() => console.log('File is opened'))
                      .catch(e => console.log('Error opening file', e));
                    })  
              })
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-02-24
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-01-31
        相关资源
        最近更新 更多