首页 > 解决方案 > 从离子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 存储在 localstorage 中。

知道如何删除文件吗?

标签: fileionic-frameworkionic4

解决方案


如果您已经有一个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
});

有关更多文档示例,请参阅这些链接。


推荐阅读