Phonegap - 如何访问www文件夹中的文件? [英] Phonegap - How to access file in www-folder?

查看:226
本文介绍了Phonegap - 如何访问www文件夹中的文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我看到多个解决方案如何访问www文件夹中的文件,但没有解决方案为我工作。我用iOS模拟器测试iOS下的应用程序。

我想要访问www文件夹中的文件 test.txt

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 the file test.txtin the www folder.

我的当前解决方案如下:

My current solution looks like this:

var filePathURI = getPhoneGapPath() + "test.txt";
window.resolveLocalFileSystemURI(filePathURI, onResolveSuccess, onFail);

function getPhoneGapPath() {  
    'use strict';
    var path = window.location.pathname;
    var phoneGapPath = path.substring(0, path.lastIndexOf('/') + 1);
    return phoneGapPath;
};

此解决方案不适用于我。我得到一个错误与 errorCode = 2 这显然意味着 FileError.SECURITY_ERR 。但我尝试, resolveLocalFileSystemURI 我无法访问该文件。


信息:我尝试下面的 filePathURI

This solution does not work for me. I get an error with errorCode = 2 which obviously means FileError.SECURITY_ERR. However I try, with resolveLocalFileSystemURI I can not access to the file.

INFO: I tried following filePathURI:


  1. /Users/UserName/Library/Application%20Support/iPhone%20Simulator/7.0/Applications /GUID/AppName.app/www/test.txt

  2. file:///Users/UserName/Library/Application%20Support/iPhone%20Simulator/7.0 /Applications/GUID/AppName.app/www/test.txt

  1. /Users/UserName/Library/Application%20Support/iPhone%20Simulator/7.0/Applications/GUID/AppName.app/www/test.txt
  2. file:///Users/UserName/Library/Application%20Support/iPhone%20Simulator/7.0/Applications/GUID/AppName.app/www/test.txt

任何人都可以给我一个工作解决方案? / p>

Can anyone give me a working solution?

推荐答案

我建议使用PhoneGap文件插件提供的 resolveLocalFileSystemURL 方法。然后,您可以使用 cordova.file.applicationDirectory 属性来访问您的 www 文件夹所在的位置。

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.

请务必安装插件: $ 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.<FileEntry>} 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);

  }

};

这篇关于Phonegap - 如何访问www文件夹中的文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆