【问题标题】:PhoneGap write to filePhoneGap 写入文件
【发布时间】:2013-11-19 19:30:10
【问题描述】:

我是 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");
}

运行此代码时,我得到了 Uncaught ReferenceError: LocalFileSystem is not defined at 使用其他 Javascript 在其他对象上给我同样的错误(例如 ActiveXObject...)

谁能帮帮我?谢谢!

【问题讨论】:

    标签: android cordova


    【解决方案1】:

    确保您拥有 /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 中。

    【讨论】:

      猜你喜欢
      • 2013-08-02
      • 1970-01-01
      • 1970-01-01
      • 2012-04-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-03-24
      • 2015-12-18
      相关资源
      最近更新 更多