【问题标题】:Delete file from file system in ionic 4从离子4中的文件系统中删除文件
【发布时间】:2019-09-02 15:43:20
【问题描述】:

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

知道如何删除文件吗??

【问题讨论】:

  • 您使用哪个插件来访问文件系统?是File plugin 吗?
  • 是的,我正在使用文件和文件打开器插件

标签: file ionic-framework ionic4


【解决方案1】:

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

查看这些链接了解更多documentationexamples

【讨论】:

    【解决方案2】:

    这是一个完美的方法(在 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')
      }
    

    【讨论】:

    猜你喜欢
    • 2022-10-18
    • 2015-12-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-02-01
    • 2018-07-22
    • 2018-12-24
    相关资源
    最近更新 更多