【问题标题】:Cordova plugin File always returns null valueCordova 插件文件总是返回空值
【发布时间】:2021-11-20 23:27:12
【问题描述】:

我的 Cordova 文件插件出现故障。 这是我创建的一项服务,用于在我的应用中管理文档下载。

问题的原因是this.file.externalRootDirectory总是返回一个空值。我不知道可能是什么原因。我感谢您的帮助。 在这里我离开我的功能。

import { FileOpener } from '@ionic-native/file-opener/ngx';
import { File } from "@ionic-native/file/ngx";

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

  async downloadFile(base64String: string, extension: string, fileName: string) {
    const dt = new Date().toISOString().slice(0, 19).split('-').join('').split(':').join('');
    fileName = `${fileName}-${dt}.${extension}`;
    const blob = this.b64toBlob(base64String, this.getMimeTypeFromExt(extension));
    if (this.platform.is('android')) {
      const path = `${this.file.externalRootDirectory + '/Download/'}`;
      this.file.writeFile(
        path,
        fileName,
        blob
      )
      .then(() => {
        this.fileOpener.open(`${path}${fileName}`, this.getMimeTypeFromExt(extension)).then( () => {
        }).catch((error) => {
          this.notificationService.error(MSG.OPENING_DOCUMENT_ERROR);
        });
      }).catch((error) => {
        this.notificationService.error(MSG.DOWNLOADED_DOCUMENT_ERROR);
      });
    } else {
      const fileURL = URL.createObjectURL(blob);
      if ( extension === 'pdf' ) {
        const newWindow = window.open();
        newWindow.location.href = fileURL;
      } else {
        const link = document.createElement("a");
        link.href = fileURL;
        link.download = fileName
        link.click();
      }
    }
  }

PS:几天前它对我来说工作正常,但它开始工作很糟糕。

【问题讨论】:

    标签: angular cordova ionic-framework ionic4


    【解决方案1】:

    您需要声明extra filesystems

    <preference name="iosExtraFilesystems" value="library,library-nosync,documents,documents-nosync,cache,bundle,root" />
    <preference name="AndroidExtraFilesystems" value="files,files-external,documents,sdcard,cache,cache-external,assets,root" />
    

    默认情况下,上面应该已经设置好了,除非你已经覆盖了。

    您可以在here查看更多详情

    【讨论】:

      猜你喜欢
      • 2019-12-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-07-13
      • 2016-07-11
      • 1970-01-01
      相关资源
      最近更新 更多