在Ionic 4中从文件系统删除文件

时间:2019-09-02 06:46:54

标签: file ionic-framework ionic4

我正在使用一个应用程序在其中创建脱机PDF并将其保存在文件系统中。 现在的问题是,当我删除特定记录时,我需要通过文件插件从文件系统中删除PDF,但是找不到与此相关的任何方法。我正在使用Ionic 4,这是一些安全的代码。

if (this.plt.is('cordova')) {
      this.pdfObj.getBuffer((buffer) => {
        const blob = new Blob([buffer], { type: 'application/pdf' });
        // Save the PDF to the data Directory of our App
        this.file.writeFile(this.file.externalRootDirectory + '/Downloads/', 'ACCSYS-' +
        this.randomString(4) + '-' + encodeURI(this.headerData.title) + '.pdf', blob, { replace: true }).then(fileEntry => {
          // Open the PDf with the correct OS tools
          setTimeout(() => {
            this.hideLoader();
            this.fileOpener.open(fileEntry.nativeURL, 'application/pdf');
            this.pdfObj = null;
          }, 1000);
        });
      });
    } else {
      setTimeout(() => {
        this.hideLoader();
        this.pdfObj.download();
        this.pdfObj = null;
      }, 500);
    }

假设我将nativeURL存储在本地存储中。

任何想法如何删除文件??

2 个答案:

答案 0 :(得分:1)

如果您已有float4对象,则可以使用fileEntry方法删除文件,如下所示:

remove()

有关更多documentationexamples的信息,请参见这些链接。

答案 1 :(得分:0)

这是执行此操作的最佳方法(在ts文件顶部定义窗口)

delete() {
    // this.fileHelper.removeFile();
    const fileToRemove = this.remoteURL; // Change this with your file path
    window.resolveLocalFileSystemURL( fileToRemove,  (dirEntry) => {
      dirEntry.remove(this.successHandler, this.errorHandler);
    });
  }
  successHandler() {
    console.log('Directory deleted successfully');
  }
  errorHandler() {
    console.log('There is some error while deleting directory')
  }
相关问题