试图检查文件是否存在,如果没有下载

时间:2015-10-22 05:23:50

标签: javascript android html cordova

大家好我正在尝试使用这个简单的代码来检查文件是否存在,如果没有下载它。但它始终只显示“正在检查数据文件”。

可能是什么原因?

我在这里找到了代码:http://www.raymondcamden.com/2014/07/01/Cordova-Sample-Check-for-a-file-and-download-if-it-isnt-there

的index.html

 <!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
        <title></title>
        <meta name="description" content="">
        <meta name="viewport" content="width=device-width">
        <link rel="stylesheet" type="text/css" href="css/app.css" />
    </head>
    <body>

    <h2>My App</h2>
    <div id="status"></div>

    <script src="cordova.js"></script>  
    <script src="js/app.js"></script>
    </body>
</html>

app.js

document.addEventListener("deviceready", init, false);

//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";

function init() {

    $status = document.querySelector("#status");

    $status.innerHTML = "Checking for data file.";

    store = cordova.file.dataDirectory;

    //Check for the file. 
    window.resolveLocalFileSystemURL(store + fileName, appStart, downloadAsset);

}

function downloadAsset() {
    var fileTransfer = new FileTransfer();
    console.log("About to start transfer");
    fileTransfer.download(assetURL, store + fileName, 
        function(entry) {
            console.log("Success!");
            appStart();
        }, 
        function(err) {
            console.log("Error");
            console.dir(err);
        });
}

//I'm only called when the file exists or has been downloaded.
function appStart() {
    $status.innerHTML = "App ready!";
}

这些只是android Studio中logcat输出的最后几行,因为在这里插入所有内容太多了:

10-22 16:46:40.927    2315-2315/? E/PGA﹕ PgaUtilsParamSize: unsuported param GL_CULL_FACE, assuming size 1
10-22 16:46:40.927    2315-2315/? E/PGA﹕ PgaUtilsParamSize: unsuported param GL_DITHER, assuming size 1

-22 16:47:48.367  30178-30897/? D/dalvikvm﹕ GC_FOR_ALLOC freed 736K, 29% free 2727K/3840K, paused 0ms, total 0ms
10-22 16:48:43.477  30178-30897/? D/dalvikvm﹕ GC_FOR_ALLOC freed 736K, 29% free 2727K/3840K, paused 0ms, total 0ms
10-22 16:48:48.827   30217-2359/? D/BstCommandProcessor-httpd﹕ command: ping
10-22 16:48:48.827   30217-2359/? D/BstCommandProcessor-httpd﹕ response: {"result":"ok"}
10-22 16:48:48.847   30217-2360/? D/BstCommandProcessor-httpd﹕ command: clipboard (with args)
10-22 16:48:48.847   30217-2360/? D/BstCommandProcessor-httpd﹕ response: {"result":"ok"}
10-22 16:49:38.587  30178-30897/? D/dalvikvm﹕ GC_FOR_ALLOC freed 736K, 29% free 2727K/3840K, paused 0ms, total 0ms
10-22 16:50:33.697  30178-30897/? D/dalvikvm﹕ GC_FOR_ALLOC freed 736K, 29% free 2727K/3840K, paused 0ms, total 0ms
10-22 16:50:42.037   30217-2361/? D/BstCommandProcessor-httpd﹕ command: ping
10-22 16:50:42.037   30217-2361/? D/BstCommandProcessor-httpd﹕ response: {"result":"ok"}
10-22 16:50:42.257   30217-2362/? W/BstCommandProcessor-httpd﹕ Reached maxLineLength limit while reading the content of a file
10-22 16:50:42.257   30217-2362/? D/BstCommandProcessor-httpd﹕ command: clipboard (with args)
10-22 16:50:42.267   30217-2362/? D/dalvikvm﹕ GC_FOR_ALLOC freed 1969K, 24% free 6849K/8896K, paused 0ms, total 0ms
10-22 16:50:42.277   30217-2362/? D/dalvikvm﹕ GC_FOR_ALLOC freed 1557K, 24% free 6850K/9004K, paused 10ms, total 10ms
10-22 16:50:42.287   30217-2362/? D/dalvikvm﹕ GC_FOR_ALLOC freed 1318K, 24% free 6854K/9004K, paused 10ms, total 10ms
10-22 16:50:42.297   30217-2362/? D/dalvikvm﹕ GC_FOR_ALLOC freed 1230K, 24% free 6851K/9012K, paused 10ms, total 10ms
10-22 16:50:42.307   30217-2362/? D/dalvikvm﹕ GC_FOR_ALLOC freed 1215K, 24% free 6852K/9012K, paused 0ms, total 0ms
10-22 16:50:42.307   30217-2362/? D/dalvikvm﹕ GC_FOR_ALLOC freed 1161K, 24% free 6858K/9012K, paused 0ms, total 0ms
10-22 16:50:42.317   30217-2362/? D/dalvikvm﹕ GC_FOR_ALLOC freed 1101K, 24% free 6862K/9012K, paused 10ms, total 10ms
10-22 16:50:42.317   30217-2362/? D/BstCommandProcessor-httpd﹕ response: {"result":"ok"}
10-22 16:51:28.807  30178-30897/? D/dalvikvm﹕ GC_FOR_ALLOC freed 736K, 29% free 2727K/3840K, paused 0ms, total 0ms
10-22 16:52:00.027  30100-30111/? D/dalvikvm﹕ GC_FOR_ALLOC freed 7538K, 38% free 12639K/20212K, paused 10ms, total 20ms
10-22 16:52:23.917  30178-30897/? D/dalvikvm﹕ GC_FOR_ALLOC freed 736K, 29% free 2727K/3840K, paused 0ms, total 0ms
10-22 16:53:19.027  30178-30897/? D/dalvikvm﹕ GC_FOR_ALLOC freed 736K, 29% free 2727K/3840K, paused 0ms, total 0ms
10-22 16:53:26.057   30217-2363/? D/BstCommandProcessor-httpd﹕ command: ping
10-22 16:53:26.057   30217-2363/? D/BstCommandProcessor-httpd﹕ response: {"result":"ok"}
10-22 16:53:26.077   30217-2364/? W/BstCommandProcessor-httpd﹕ Reached maxLineLength limit while reading the content of a file
10-22 16:53:26.077   30217-2364/? D/BstCommandProcessor-httpd﹕ command: clipboard (with args)
10-22 16:53:26.097   30217-2364/? D/dalvikvm﹕ GC_FOR_ALLOC freed 1795K, 25% free 6825K/9020K, paused 10ms, total 10ms
10-22 16:53:26.097   30217-2364/? D/dalvikvm﹕ GC_FOR_ALLOC freed 1489K, 25% free 6824K/9020K, paused 0ms, total 0ms
10-22 16:53:26.117   30217-2364/? D/dalvikvm﹕ GC_FOR_ALLOC freed 1352K, 25% free 6825K/9020K, paused 10ms, total 10ms
10-22 16:53:26.117   30217-2364/? D/dalvikvm﹕ GC_FOR_ALLOC freed 1251K, 25% free 6836K/9020K, paused 0ms, total 0ms
10-22 16:53:26.127   30217-2364/? D/BstCommandProcessor-httpd﹕ response: {"result":"ok"}
10-22 16:54:14.137  30178-30897/? D/dalvikvm﹕ GC_FOR_ALLOC freed 736K, 29% free 2727K/3840K, paused 0ms, total 0ms
10-22 16:54:23.877   30217-2365/? D/BstCommandProcessor-httpd﹕ command: ping
10-22 16:54:23.877   30217-2365/? D/BstCommandProcessor-httpd﹕ response: {"result":"ok"}
10-22 16:54:23.887   30217-2366/? D/BstCommandProcessor-httpd﹕ command: clipboard (with args)
10-22 16:54:23.887   30217-2366/? D/BstCommandProcessor-httpd﹕ response: {"result":"ok"}
10-22 16:54:30.017  29999-30062/? D/ConnectivityService﹕ Sampling interval elapsed, updating statistics ..
10-22 16:54:30.017  29999-30062/? D/ConnectivityService﹕ Done.
10-22 16:54:30.017  29999-30062/? D/ConnectivityService﹕ Setting timer for 720seconds
10-22 16:54:30.197  29999-30062/? D/ConnectivityService﹕ handleInetConditionChange: net=1 != default=9 - ignore
10-22 16:55:09.247  30178-30897/? D/dalvikvm﹕ GC_FOR_ALLOC freed 736K, 29% free 2727K/3840K, paused 0ms, total 0ms
10-22 16:55:41.237  29999-29999/? I/EntropyMixer﹕ Writing entropy...
10-22 16:56:04.357  30178-30897/? D/dalvikvm﹕ GC_FOR_ALLOC freed 736K, 29% free 2727K/3840K, paused 0ms, total 0ms
10-22 16:56:59.467  30178-30897/? D/dalvikvm﹕ GC_FOR_ALLOC freed 736K, 29% free 2727K/3840K, paused 0ms, total 0ms

1 个答案:

答案 0 :(得分:0)

@ Taurus22, 您可能正在使用最新版本的编译器。这是Cordova / Phonegap新开发人员的常见问题。您有whitelist个问题。

修复了许多常见的白名单问题

另一种选择就是快速修复 - 但要知道此快速修复会消除white-list的所有需求。这会创建一个您可能不希望通过的security issue

快速修正将此添加到config.xml PHONEGAP BUILD ONLY
<preference name="phonegap-version" value="3.7.0" />

答案很长:

来自Top Mistakes by Developers new to Cordova/Phonegap你已经点击了:

  • #6 未为您的编译器设置“phonegap版本”
  • #7 没有为您的插件设置“版本”
  • #10 未在config.xml中添加新的“white-list”和“white-list plugin”参数。

对于#6&amp; #7

  

使用CLI版本,如果您没有为您的平台分配版本,或者如果您未在config.xml中设置phonegap-version,则在“Phonegap Build”中,您将获得最新版本。如果运气好,您的程序就会按预期运行。如果你不幸运,你会得到一系列级联错误。

     

幸运的是,对于我们所有人来说,Holly Schinsky写了一篇很好的博文来解释这一切:

     

Cordova / PhoneGap版本混淆
  http://devgirl.org/2014/11/07/cordovaphonegap-version-confusion/

#10

  

这个相对*新*要求意味着 - 要访问网络上的任何网站或资源,您必须使用白名单和白名单插件。如果您使用cordova-android@4.0.0或更高版本,此要求将生效;包括cli-5.1.1和cli-5.2.0。但是,如果您的版本在4.0.0之前,假设为3.5.0或3.7.0,则必须添加 white-list 要求。

     

要明确的是,“白名单”已经存在了一段时间,但插件和要求非常新。正如您所料,当添加“白名单”时,不推荐使用事实上的开放访问功能。或者说另一种方式,事实上开放访问功能已被计划并计划被淘汰。此更改标志着删除开放访问功能的一个步骤。

     

此外,内容安全策略(CSP)已经吸引了众多开发人员 - 因为它的公开性很差。 根据您的使用情况和您使用的Phonegap版本,CSP需要进入您使用的每个HTML页面,就像您必须等待'deviceready'一样。但是,有些情况根本不需要它。文档让一些人感到困惑,请仔细阅读。文档隐藏在许多最新文档页面的底部。

     

最后,Raymond Camden在他的博客中指出LARGE change in policy starting with Cordova 5

相关链接

  

Phonegap Build Forum:Notes for upgrading to cli-5.1.1 on PGB,现在需要白名单

     
相关问题