How to check a file's existence in phone directory with phonegap

后端 未结 14 1994
太阳男子
太阳男子 2020-12-05 10:49

I\'m writing an Android application with Phonegap 1.4.1 and Sencha that downloads and reads pdf files. How can I check whether the file exists in the phone directory using e

14条回答
  •  伪装坚强ぢ
    2020-12-05 11:33

    This code can be used for custom cases, full documentation here: download if not exist

    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!";
    }
    

提交回复
热议问题