0

I'm trying to learn phonegap using the "PhoneGap Mobile Application Development Cookbook", published by PACKT publishing. Chapter 2 discusses file systems and this seems to be giving some errors. I copied the sample code and I get the following error:

enter image description here

"NetworkStatusxxxxxxxxxx"] (the number is different every time). I've been looking for a solution for several hours now, but without success, who can help?!

My code:

!DOCTYPE html>

File Download

var downloadDirectory;

document.addEventListener("deviceready", onDeviceReady, true);

function onDeviceReady() {
    window.requestFileSystem(
        LocalFileSystem.PERSISTENT, 
        0, 
        onFileSystemSuccess, 
        null
    );

    x$('#download_btn').on( 'click', function(e) {
        download();
    });
}

function onFileSystemSuccess(fileSystem) {
    fileSystem.root.getDirectory('my_downloads',{create:true},
        function(dir) {
            downloadDirectory = dir;
        },fail);
}

function fail(error) {
    x$('#message').html('We encountered a problem: ' + error.code);
}

function download() {
    var fileURL = document.getElementById('file_url').value;
    var localFileName = getFilename(fileURL);

    x$('#message').html('Downloading ' + localFileName);

    var fileTransfer = new FileTransfer();
    fileTransfer.download(fileURL, downloadDirectory.fullPath + '/' + localFileName, 
        function(entry){
            x$('#message').html('Download complete. File saved to: ' + entry.fullPath);
        }, 
        function(error){
            alert("Download error source " + error.source);
        }
    );
}

// Obtain the filename
function getFilename(url) {
   if (url) {
      var m = url.toString().match(/.*\/(.+?)\./);
      if (m && m.length > 1) {
         return m[1] + '.' + url.split('.').pop();
      }
   }
   return "";
}
</script>

<input type="text"      id="file_url"       value="http://blogs.adobe.com/adobeingovernment/files/2012/07/phonegap.jpg" />
<input type="button"    id="download_btn"   value="Download" />

<div id="message"></div>

4

2 回答 2

1

当您在 cordova ios 项目中使用 android 版本的 cordova.js 时会发生这种情况。创建一个新的 ios cordova 项目,从创建的项目中复制 cordova-xxxjs 并放置在您现有的项目中,替换已经存在的 cordova-xxxjs 文件。然后,从 Xcode 中清理项目并再次构建。瞧!从现在开始没有警报。

于 2014-05-21T10:40:47.083 回答
1

如果加载 phonegap javascript 文件时出错,通常会发生这种情况。多次让我感到困扰的一件事是使用了错误的cordova.js文件——如果你正在构建一个 Android 应用程序,你必须使用 Android 生成的cordova.js文件,这同样适用于 iOS 应用程序。

于 2013-03-14T23:15:45.790 回答