下载到本地存储android phonegap 3.4.0

时间:2014-05-16 14:46:15

标签: jquery-mobile cordova cordova-3

我有以下代码将jpg下载到我的本地存储中,但我没有下载任何文件 我安装了以下插件

  • cordova插件添加org.apache.cordova.file-transfer
  • cordova插件添加org.apache.cordova.file

    <!DOCTYPE html>
    <html>
    <head>
        <meta charset="utf-8" />
        <meta name="format-detection" content="telephone=no" />
        <!-- WARNING: for iOS 7, remove the width=device-width and height=device-height attributes. See https://issues.apache.org/jira/browse/CB-4323 -->
        <meta name="viewport" content="user-scalable=no, initial-scale=1, maximum-scale=1, minimum-scale=1, width=device-width, height=device-height, target-densitydpi=device-dpi" />
        <link rel="stylesheet" type="text/css" href="css/index.css" />
        <title>Hello World</title>
    
    </head>
      <body>
    
    <script type="text/javascript" src="cordova.js"></script>
    
    
    <script type="text/javascript" src="painlessfs.js"></script>
    <script type="text/javascript">
    
    var url = 'http://2d-code.co.uk/images/google-qr-code.jpg';
    var filePath = 'file:///android_asset/www/res/db/"';
    var fileTransfer = new FileTransfer();
    var uri = encodeURI(url);
    
    fileTransfer.download(
        uri,
        filePath,
        function(entry) {
            alert("download complete: " + entry.fullPath);
        },
        function(error) {
            alert("download error source " + error.source);
            alert("download error target " + error.target);
            alert("upload error code" + error.code);
        },
        false,
        {
            headers: {
                "Authorization": "Basic dGVzdHVzZXJuYW1lOnRlc3RwYXNzd29yZA=="
            }
        }
    );
    
    </script>
    

在此之后我运行了phonegap build android并在我的设备中安装了我的apk并没有收到任何警报。请帮助

1 个答案:

答案 0 :(得分:1)

您必须在deviceReadyevent触发时运行代码:

document.addEventListener("deviceReady", deviceReady, false);

function deviceReady() {
        //Your code goes here
}

它应该是这样的:

document.addEventListener("deviceReady", deviceReady, false);

function deviceReady() {
    var url = 'http://2d-code.co.uk/images/google-qr-code.jpg';
    var filePath = 'file:///android_asset/www/res/db/"';
    var fileTransfer = new FileTransfer();
    var uri = encodeURI(url);

    fileTransfer.download(
        uri,
        filePath,
        function(entry) {
            alert("download complete: " + entry.fullPath);
        },
        function(error) {
            alert("download error source " + error.source);
            alert("download error target " + error.target);
            alert("upload error code" + error.code);
        },
        false,
        {
            headers: {
                "Authorization": "Basic dGVzdHVzZXJuYW1lOnRlc3RwYXNzd29yZA=="
            }
        }
    );
}

详细了解here