【发布时间】:2015-03-12 12:01:02
【问题描述】:
我们必须使用 PhoneGap Build 和 Apache Cordova 为 iOS 和 Android 构建应用程序。 Phonegap 版本是 3.5.0。我们想在互联网连接可用时更新应用程序。所以我们需要从在线服务器下载一些图像文件到本地文件系统到应用程序(iOS和Android)。 这是使用的示例 JavaScript 代码:
try{
//The directory to store data
var store;
//Used for status updates
var $status;
//URL of our asset
var assetURL = "https://raw.githubusercontent.com/cfjedimaster/Cordova-Examples/master/readme.md";
//File name of our important data file we didn't ship with the app
var fileName = "mydatafile.txt";
//////////////////////
alert("Checking for data file.");
//Check for the file.
window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, onFileSystemSuccess, onError);
/////////////////////////////
} catch(e){
alert(e.message);
}
function downloadAsset() {
var fileTransfer = new FileTransfer();
alert("About to start transfer");
fileTransfer.download(assetURL, store + fileName,
function(entry) {
alert("Success!");
appStart();
},
function(err) {
alert("Error!");
console.dir(err);
alert(err);
});
}
//I'm only called when the file exists or has been downloaded.
function appStart() {
// $status.innerHTML = "App ready!";
alert( "App ready!");
}
function onFileSystemSuccess() {
try{
store = cordova.file.dataDirectory;
//Check for the file.
window.resolveLocalFileSystemURL(store + fileName, appStart, downloadAsset);
} catch(e){
alert(e.message);
}
}
function onError(){
alert('error');
}
启动应用程序时,结果是 2 个警报:
无法读取 undefined 的属性“dataDirectory”
// alert(e.message);错误
// alert('Error');
【问题讨论】:
-
window.requestFileSystem是否在 deviceReady 被触发后执行? -
是的,它在 deviceready 事件触发后执行。
-
您是否为该平台正确安装了文件插件?您可能使用的是 PhoneGap Build,还是只使用普通的 Cordova CLI?
-
Phonegap Build 的 config.xml 文件中使用了 File 插件。