【发布时间】:2014-01-22 12:41:02
【问题描述】:
我正在尝试使用 Phonegap[cordova 3.3.0] 在 IOS 上处理文件。我阅读了如何访问文件并在电话间隙的 API 文档中阅读它们。还添加了这样的插件
$ cordova plugin add org.apache.cordova.file
$ cordova plugin ls
[ 'org.apache.cordova.file' ]
$ cordova plugin rm org.apache.cordova.file
$ cordova plugin add org.apache.cordova.file-transfer
$ cordova plugin ls
[ 'org.apache.cordova.file',
'org.apache.cordova.file-transfer' ]
$ cordova plugin rm org.apache.cordova.file-transfer
函数 gotFS(fileSystem) 在 onDeviceReady() 函数之后没有调用。
这是我正在使用的代码:
function onDeviceReady() {
window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, gotFS, fail);
}
function gotFS(fileSystem) {
fileSystem.root.getFile("readme.txt", null, gotFileEntry, fail);
}
function gotFileEntry(fileEntry) {
fileEntry.file(gotFile, fail);
}
function gotFile(file){
readDataUrl(file);
readAsText(file);
}
function readDataUrl(file) {
var reader = new FileReader();
reader.onloadend = function(evt) {
console.log("Read as data URL");
console.log(evt.target.result);
};
reader.readAsDataURL(file);
}
function readAsText(file) {
var reader = new FileReader();
reader.onloadend = function(evt) {
console.log("Read as text");
console.log(evt.target.result);
};
reader.readAsText(file);
}
function fail(evt) {
console.log(evt.target.error.code);
}
此代码适用于 android。但是对于 Ios,我得到 ReferenceError: Can't find variable: LocalFileSystem 在这一行 -
window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, gotFS, fail);
【问题讨论】:
-
你真的会监听deviceready事件来调用onDeviceReady函数吗?您是否尝试在每个步骤中添加 console.log 以确定是否调用了什么?
-
是的 deviceready 事件正在调用。此行有问题 window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, gotFS, fail);
-
此代码适用于 android。但是对于 Ios,我得到 ReferenceError: Can't find variable: LocalFileSystem in this line - window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, gotFS, fail);
-
您显示的有关插件安装的行包含“cordova plugin rm”,我希望您没有运行它。还有你是如何启动你的项目的?
-
是的,我没有运行 cordova plugin rm.As rm for remove 所以没有运行那行。我正在为设备(ipad)提供午餐项目。
标签: javascript ios cordova