Writing and reading file in phonegap

后端 未结 4 1571
傲寒
傲寒 2020-12-09 22:49

I tried writing/reading a file in phonegap+android, here is the set up:

$(document).ready(function() {
    document.addEventListene         


        
4条回答
  •  伪装坚强ぢ
    2020-12-09 23:21

    Here is what I came up with based on several links. I had been searching to do this as well. I used this site as a reference http://www.digitalnoiz.com/mobile-development/mobile-file-explorer-with-phonegapcordova-and-jquery-mobile-part-1/ as well as the Phonegap document api references

    function displayMessage(msg)
    {
        navigator.notification.alert(msg);
    }
    
    function loadDirectories(fileSystem)
    {
        directoryEntry = fileSystem.root;
    
        var directoryReader = directoryEntry.createReader();
    
        directoryReader.readEntries(function(entries){
                var sOutput = "";
                for(var i=0; i < entries.length; i++)
                {
                    if(!entries[i].isDirectory)
                    {
                        fileSystem.root.getFile(entries[i].name,null,gotFileEntry,fail);
                    }
                }                            
                //displayMessage(sOutput);
            },fail);
    }
    function gotFileEntry(fileEntry)
    {
        fileEntry.file(function(file){
            var reader = new FileReader();
            reader.onloadend = function(evt){
            displayMessage(evt.target.result);
        };
    reader.readAsText(file);                        
        },fail);
    }
    function failFile(evt)
    {
        displayMessage(evt.target.error.code);
    }
    function fail(error)
    {
        displayMessage("Failed to list directory contents: " + error.code);
    }   
    function onBodyLoad()
    {       
    document.addEventListener("deviceready", onDeviceReady, false);
    }       
    function onDeviceReady()
    {
        window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, loadDirectories, fail);
    }
    

提交回复
热议问题