从离子4中的文件系统中删除文件

Delete file from file system in ionic 4

我正在使用一个应用程序创建离线 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 存储在本地存储中。

知道如何删除文件吗??

如果您已经有一个 fileEntry 对象,您可以使用 remove() 方法删除文件,如下所示:

fileEntry.remove(function() {
    // if the file has been successfully removed
}, function(error) {
    // if there was an error removing the file
}, function() {
    // if the file does not exist
});

查看这些链接了解更多信息 documentation and examples

这是完美的方法(在 ts 文件顶部定义 window)

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')
  }