PhoneGap写入文件

时间:2013-11-19 11:02:42

标签: android cordova

我是PhoneGap的新手,并尝试开发一个简单的应用程序来写入文本文件。我使用过使用LocalFileSystem的PhoneGap文档。

// Cordova is loading
document.addEventListener("deviceready", onDeviceReady, false); // PhoneGap is fully loaded

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

function fail(){
    console.log('fail');
}

function gotFS(fileSystem) {
    var path = "test.txt";
    fileSystem.root.getFile(path, {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");
}

运行此代码时,我得到了一个未捕获的ReferenceError:未定义LocalFileSystem 使用其他Javascript在其他对象(例如ActiveXObject ...)

上给出了相同的错误

任何人都可以帮助我吗?谢谢!

1 个答案:

答案 0 :(得分:0)

确保您拥有/res/xml/config.xml中的功能:

    <feature name="File">
        <param name="android-package" value="org.apache.cordova.FileUtils" />
    </feature>
    <feature name="FileTransfer">
        <param name="android-package" value="org.apache.cordova.FileTransfer" />
    </feature>

还有AndroidManifest.xml中的权限:

    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

ActiveXObject仅存在于Internet Explorer中。