带有base 64的文档查看器

时间:2017-11-06 09:22:18

标签: angular cordova cordova-plugins ionic3

我尝试将此插件用于我的Ionic 3应用:https://ionicframework.com/docs/native/document-viewer/1

但我想将它与base 64编码文件一起使用。

我找到了一个解决方案:我在设备中下载文件,然后使用文档查看器打开它。

    savebase64AsPDF(filename,content, folderpath = cordova.file.externalRootDirectory){
    filename = filename.replace(/ /g,"_");
    console.log(filename);
    if(!filename.endsWith('.pdf')) {
    }
    let contentType = "application/pdf";

    // Convert the base64 string in a Blob
    let DataBlob = this.b64toBlob(content,contentType);

    window['resolveLocalFileSystemURL'](folderpath, dir => {
      dir.getFile(filename, {create:true}, file => {
        file.createWriter(fileWriter => {
          fileWriter.write(DataBlob);
          let alert: any = this.alertCtrl.create({title: `'${filename}' a bien été enregistré`,
            subTitle : `Il se trouve dans les documents de votre appareil`, buttons: [this.translate.get('close')]});
          alert.present();
          const options: DocumentViewerOptions = {
            title: filename
          };
          this.docView.viewDocument(folderpath + filename, 'application/pdf', options);
        }, function(){
          let alert: any = this.alertCtrl.create({title: `'${filename}' n'a pas été enregistré`,
            subTitle : `Une erreur est survenue durant la sauvegarde du fichier`, buttons: [this.translate.get('close')]});
        });
      });
    });
  }

效果很好!但只有一次......

现在我总是有这个错误:

"org.json.JSONException: mkdirs /data/user/0/com.openxtrem.myapp/cache/tmp/DocumentViewerPlugin failed.
    at de.sitewaerts.cordova.documentviewer.DocumentViewerPlugin.getAccessibleFileNew(DocumentViewerPlugin.java:750)
    at de.sitewaerts.cordova.documentviewer.DocumentViewerPlugin.getAccessibleFile(DocumentViewerPlugin.java:706)
    at de.sitewaerts.cordova.documentviewer.DocumentViewerPlugin._open(DocumentViewerPlugin.java:487)
    at de.sitewaerts.cordova.documentviewer.DocumentViewerPlugin.doExecute(DocumentViewerPlugin.java:312)
    at de.sitewaerts.cordova.documentviewer.DocumentViewerPlugin.access$000(DocumentViewerPlugin.java:49)
    at de.sitewaerts.cordova.documentviewer.DocumentViewerPlugin$1.run(DocumentViewerPlugin.java:185)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1133)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:607)
    at java.lang.Thread.run(Thread.java:776)
Caused by: java.io.IOException: mkdirs /data/user/0/com.openxtrem.myapp/cache/tmp/DocumentViewerPlugin failed.
    at de.sitewaerts.cordova.documentviewer.DocumentViewerPlugin.getAccessibleFileNew(DocumentViewerPlugin.java:736)
    ... 8 more

我真的不知道我要做些什么来解决这个问题。有人有解决方案吗?

0 个答案:

没有答案