Phonegap 3.5.0 Cordova File API错误:无法读取未定义的属性'dataDirectory'

我们必须使用PhoneGap Build和Apache Cordova为iOS和Android构build一个应用程序。 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个警报:

  • 无法读取未定义的 // alert(e.message);属性'dataDirectory' // alert(e.message);

  • 错误/ / // alert('Error');

好的,我们发现问题了! 在应用程序内部,我们已经使用包含在应用程序的js文件夹中的已下载版本的cordova插件包含了cordova js文件。 所以,而不是以下:

 <script type="text/javascript" src="js/cordova.js"></script> 

build议input:

 <script type="text/javascript" src="cordova.js"></script> 

Phongap会自己添加cordova文件。

这可能听起来很愚蠢,但对于各种“不能读取财产”的问题,你也可以试试

rm -rf plugins
cordova/ionic platform remove xxx
cordova/ionic platform add xxx

它解决了我根本没有任何意义的问题。 谁会期望插件本身出错?