PhoneGap实例

    <!DOCTYPE HTML>
    <html>

    <head>
    <meta name="viewport" content="width=320; user-scalable=no" />
    <meta http-equiv="Content-type" content="text/html; charset=utf-8">
    <title>Minimal AppLaud App</title>

    <script type="text/javascript" charset="utf-8" src="phonegap-1.4.1.js"></script>
    <script type="text/javascript" charset="utf-8">
    var fileSystem;

    //generic getById
    function getById(id) {
        return document.querySelector(id);
    }
    //generic content logger
    function logit(s) {
        getById("#content").innerHTML += s;
    }

    //generic error handler
    function onError(e) {
        getById("#content").innerHTML = "<h2>Error</h2>"+e.toString();
    }

    function doDeleteFile(e) {
        fileSystem.root.getFile("test.txt", {create:true}, function(f) {
            f.remove(function() {
                logit("File removed<p/>");
            });
        }, onError);
    }

    function metadataFile(m) {
        logit("File was last modified "+m.modificationTime+"<p/>");   
    }

    function doMetadataFile(e) {
        fileSystem.root.getFile("test.txt", {create:true}, function(f) {
            f.getMetadata(metadataFile,onError);
        }, onError);
    }

    function readFile(f) {
        reader = new FileReader();
        reader.onloadend = function(e) {
            console.log("go to end");
            logit("<pre>" + e.target.result + "</pre><p/>");
        }
        reader.readAsText(f);
    }

    function doReadFile(e) {
        fileSystem.root.getFile("test.txt", {create:true}, readFile, onError);
    }

    function appendFile(f) {

        f.createWriter(function(writerOb) {
            writerOb.onwrite=function() {
                logit("Done writing to file.<p/>");
            }
            //go to the end of the file...
            writerOb.seek(writerOb.length);
            writerOb.write("Test at "+new Date().toString() + "\n");
        })

    }

    function doAppendFile(e) {
        fileSystem.root.getFile("test.txt", {create:true}, appendFile, onError);
    }

    function gotFiles(entries) {
        var s = "";
        for(var i=0,len=entries.length; i<len; i++) {
            //entry objects include: isFile, isDirectory, name, fullPath
            s+= entries[i].fullPath;
            if (entries[i].isFile) {
                s += " [F]";
            }
            else {
                s += " [D]";
            }
            s += "<br/>";
            
        }
        s+="<p/>";
        logit(s);
    }

    function doDirectoryListing(e) {
        //get a directory reader from our FS
        var dirReader = fileSystem.root.createReader();

        dirReader.readEntries(gotFiles,onError);        
    }

    function onFSSuccess(fs) {
        fileSystem = fs;

        getById("#dirListingButton").addEventListener("touchstart",doDirectoryListing);            
        getById("#addFileButton").addEventListener("touchstart",doAppendFile);            
        getById("#readFileButton").addEventListener("touchstart",doReadFile);            
        getById("#metadataFileButton").addEventListener("touchstart",doMetadataFile);            
        getById("#deleteFileButton").addEventListener("touchstart",doDeleteFile);            
       
        logit( "Got the file system: "+fileSystem.name +"<br/>" +
                                        "root entry name is "+fileSystem.root.name + "<p/>")   

        doDirectoryListing();
    }

    function onDeviceReady() {

        //request the persistent file system
        window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, onFSSuccess, onError);
       
    }

    function init() {
        document.addEventListener("deviceready", onDeviceReady, true);
    }
    </script>

    <style>
    button { width: 100%; padding: 5px; }
    </style>
    </head>

    <body οnlοad="init();" id="stage" class="theme">

    <button id="addFileButton">Create/Append to Test File</button>
    <button id="readFileButton">Read Test File</button>
    <button id="metadataFileButton">Get Test File Metadata</button>
    <button id="deleteFileButton">Delete Test File</button>
    <button id="dirListingButton">Show Directory Contents</button>

    <div id="content"></div>

    </body>
    </html>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值