2013-03-21 60 views

回答

5

像伊詹说,有没有办法来港现有的本地应用程序的工作灯的混合应用。但是,您可以利用,该工具适用于Android和iOS等不同环境中的Worklight混合应用程序。如果您创建了Cordova Plugin,则需要为您希望支持的所有环境创建一个插件。

这里的文件I/O API为Writing a file一个简单的例子:

// Wait for Cordova to load 
// 
document.addEventListener("deviceready", onDeviceReady, false); 

// Cordova is ready 
// 
function onDeviceReady() { 
    window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, gotFS, fail); 
} 

function gotFS(fileSystem) { 
    fileSystem.root.getFile("readme.txt", {create: true, exclusive: false}, gotFileEntry, fail); 
} 

function gotFileEntry(fileEntry) { 
    fileEntry.createWriter(gotFileWriter, fail); 
} 

function gotFileWriter(writer) { 
    writer.onwriteend = function(evt) { 
     console.log("contents of file now 'some sample text'"); 
     writer.truncate(11); 
     writer.onwriteend = function(evt) { 
      console.log("contents of file now 'some sample'"); 
      writer.seek(4); 
      writer.write(" different text"); 
      writer.onwriteend = function(evt){ 
       console.log("contents of file now 'some different text'"); 
      } 
     }; 
    }; 
    writer.write("some sample text"); 
} 

function fail(error) { 
    console.log(error.code); 
} 

下面是Reading一个例子文件:

// Wait for Cordova to load 
// 
function onLoad() { 
    document.addEventListener("deviceready", onDeviceReady, false); 
} 

// Cordova is ready 
// 
function onDeviceReady() { 
    window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, gotFS, fail); 
} 

function gotFS(fileSystem) { 
    fileSystem.root.getFile("readme.txt", null, gotFileEntry, fail); 
} 

function gotFileEntry(fileEntry) { 
    fileEntry.file(gotFile, fail); 
} 

function gotFile(file){ 
    readDataUrl(file); 
    readAsText(file); 
} 

function readDataUrl(file) { 
    var reader = new FileReader(); 
    reader.onloadend = function(evt) { 
     console.log("Read as data URL"); 
     console.log(evt.target.result); 
    }; 
    reader.readAsDataURL(file); 
} 

function readAsText(file) { 
    var reader = new FileReader(); 
    reader.onloadend = function(evt) { 
     console.log("Read as text"); 
     console.log(evt.target.result); 
    }; 
    reader.readAsText(file); 
} 

function fail(evt) { 
    console.log(evt.target.error.code); 
} 
相关问题