gpt4 book ai didi

javascript - Phonegap - 如何访问 www 文件夹中的文件?

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:07:37 24 4
gpt4 key购买 nike

我看到了多种如何访问 www 文件夹中的文件的解决方案,但没有一种解决方案适合我。我使用 iOS 模拟器测试 iOS 下的应用程序。
我想访问 www 文件夹中的文件 test.txt

我目前的解决方案是这样的:

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:

  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

谁能给我一个可行的解决方案?

最佳答案

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

确保安装插件:$ cordova plugin add org.apache.cordova.file

然后您可以使用如下对象来解析文件并执行所需的任何操作:

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);

}

};

关于javascript - Phonegap - 如何访问 www 文件夹中的文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21880109/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com