【问题标题】:Error: Uncaught (in promise): [object Object] with Ionic native file plugin错误:未捕获(承诺中):带有 Ionic 本机文件插件的 [object Object]
【发布时间】:2019-04-09 18:03:29
【问题描述】:

我需要使用 blob 创建一个 pdf,并且我已关注 this blog。但它不起作用。你能告诉我为什么吗?

注意:在此行 const fileEntry = await 上发出错误。这是在android device。在浏览器上,它工作正常(即this.pdfObj.download();)。

  constructor(
    private file: File,
    private platform: Platform,
    private fileOpener: FileOpener,
  ) {
  }

downloadReport() {
    this.createPdf();
    if (this.platform.is('cordova')) {
      try {
        this.pdfObj.getBuffer(async (buffer) => {
          var utf8=new Uint8Array(buffer);
          var binaryArray=utf8.buffer;
          var blob = new Blob([binaryArray], { type: 'application/pdf' });

          const fileEntry = await this.file.writeFile(this.file.dataDirectory, 'myletter.pdf', blob, { replace: true });
          this.fileOpener.open(this.file.dataDirectory + 'myletter.pdf', 'application/pdf');
        });
      }
      catch (err) {
        console.log(err);
      }

    } else {
      // On a browser simply use download!
      this.pdfObj.download();
    }
  }  



createPdf() {
    var docDefinition = {
      content: [
        { text: 'REMINDER', style: 'header' },
        { text: new Date().toTimeString(), alignment: 'right' },

        { text: 'From', style: 'subheader' }, { text: 'from' },

        { text: 'To', style: 'subheader' }, 'To',

        { text: 'Sam lokuge', style: 'story', margin: [0, 20, 0, 20] },

        {
          ul: [
            'Bacon',
            'Rips',
            'BBQ',
          ]
        }
      ],
      styles: {
        header: {
          fontSize: 18,
          bold: true,
        },
        subheader: {
          fontSize: 14,
          bold: true,
          margin: [0, 15, 0, 0]
        },
        story: {
          italic: true,
          alignment: 'center',
          width: '50%',
        }
      }
    }
    this.pdfObj = pdfMake.createPdf(docDefinition);
  }

这是错误:

 core.js:1350 ERROR Error: Uncaught (in promise): [object Object]
        at c (polyfills.js:3)
        at polyfills.js:3
        at rejected (assessment-report.module.ts:13)
        at t.invoke (polyfills.js:3)
        at Object.onInvoke (core.js:4629)
        at t.invoke (polyfills.js:3)
        at r.run (polyfills.js:3)
        at polyfills.js:3
        at t.invokeTask (polyfills.js:3)
        at Object.onInvokeTask (core.js:4620)

【问题讨论】:

    标签: angular typescript ionic3 cordova-plugins ionic-native


    【解决方案1】:

    我在这里找到了问题。那是由于这条路径this.file.dataDirectory。但是错误消息没有明确提及。所以花了很多时间来确定问题。最后,我改变了路径,然后没有问题。我已经使用了这条路径this.file.externalApplicationStorageDirectory,现在它工作正常。

    【讨论】:

      猜你喜欢
      • 2019-08-20
      • 1970-01-01
      • 1970-01-01
      • 2017-05-22
      • 1970-01-01
      • 1970-01-01
      • 2020-04-16
      • 2019-03-08
      • 2018-10-12
      相关资源
      最近更新 更多