Phonegap - How to access file in www-folder?

前端 未结 5 833
终归单人心
终归单人心 2021-01-06 12:11

I saw multiple solutions how to access a file in the www folder but no solution works for me. I test the application under iOS with the iOS-simulator.
I want to access

5条回答
  •  礼貌的吻别
    2021-01-06 12:49

    I would suggest utilizing the resolveLocalFileSystemURL method provided by PhoneGap's file plugin. You can then use the cordova.file.applicationDirectory property to access where your www folder is located.

    Make sure you install the plugin: $ cordova plugin add org.apache.cordova.file

    Then you could use an object such as the following to parse the files and do whatever is needed:

    var FileManager = {
    
      /**
       * Execute this.entryHandler against all files and directories in phonegap's www folder
       */
      run: function () {
    
        window.resolveLocalFileSystemURL(
          cordova.file.applicationDirectory + 'www/',
          this.directoryFoundHandler,
          this.errorHandler
        );
    
      },
    
      /**
       * The directory has been successfully read.  Now read the entries.
       *
       * @param {DirectoryEntry} directoryEntry
       */
      directoryFoundHandler: function (directoryEntry) {
    
        var directoryReader = directoryEntry.createReader();
    
        directoryReader.readEntries(
          this.entryHandler,
          this.errorHandler
        );
    
      },
    
      /**
       * Files were successfully found.  Parse them!
       *
       * @param {Array.} entries
       */
      entryHandler: function (entries) {
    
        entries.forEach(function (entry) {
    
          // Deal with your files here
          if (entry.isDirectory) {
            // It's a directory might need to loop through again
          } else {
            // It's a file, do something
          }
    
        });
    
      },
    
    
      /**
       * @param {FileError} error
       */
      errorHandler: function (error) {
    
        console.log("ERROR", error);
    
      }
    
    };
    

提交回复
热议问题