文件读取器无法在PhoneGap Windows中工作

File reader not working PhoneGap Windows

本文关键字:PhoneGap Windows 工作 读取 文件      更新时间:2023-09-26

我正在使用cordova在Visual Studio 2012上开发一个Windows应用程序。我试着创建了一个.txt文件,然后在阅读了该文件的内容后。

这就是我尝试过的,

    document.addEventListener("deviceready", onDeviceReady, false);
    function onDeviceReady() {
       window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, gotFS, fail);
    }
    function gotFS(fileSystem) {
       // alert("File created");
       fileSystem.root.getFile("textfile.txt", {create: true, exclusive: false}, gotFileEntry, fail);
    }
    function gotFileEntry(fileEntry) {
      // alert(fileEntry.fullPath);
      fileEntry.createWriter(gotFileWriter, fail); 
    }
    function gotFileWriter(writer) {
      /*writer.onwrite = function(evt) {
        fileRead();
        alert("write success");
        console.log("write success");
       }; */
      /* writer.onerror = function(evt) {
        alert("Writing abort");
      }; */
      /* writer.onprogress = function(evt) {
        alert("Progress writing file");
      }; */
      writer.onwritestart = function(parents) {
        // alert("Starts" + parents);
      } 
      writer.onwriteend = function(parent) { 
        alert("Success or Fail "+ parent.target);
      };
      writer.write("some sample text");
      // alert("Writing Data" + writer);
      // contents of file now 'some sample text'
      writer.truncate(11);
      // contents of file now 'some sample'
      writer.seek(4);
      // contents of file still 'some sample' but file pointer is after the 'e' in 'some'
      writer.write(" different text");
      // contents of file now 'some different text'

    }

    function fileRead(){
      //alert("inside file read");
      fileSystem.root.getFile('textfile.txt', {create: false, exclusive: false}, gotFileEntryForRead, fail);
    }

    function gotFileEntryForRead(fileEntry) {
      alert(fileEntry);
      //alert("inside got file entry for read");
      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) {
      //alert("inside readAsText");
      var reader = new FileReader();
      reader.onloadend = function(evt) {
        if(evt.target.result == null) {
           alert("File doesn't exists");  
        } else {
           alert("File exists");
        }       
        alert(evt.target.result);
        console.log("Read as text");
        console.log(evt.target.result);
      };
      reader.readAsText(file);
    }
    function fail(evt) {
      alert("Fail");
      //alert("Error:" + evt.target.error.code);
      //console.log(evt.target.error.code);
    } 

该文件是在调用属性onwrite时创建的,但当我尝试使用警报alert(evt.target.result);查看文件的内容时,什么都没有发生。

请帮助

感谢

您在gotFileEntry、onDeviceReady和gotFS函数中使用了fail函数。这些函数执行requestFileSystem、getFile和writeFile功能。你将如何调试错误所在。因此,编写它们单独的失败处理程序函数可以轻松地处理错误,然后修复错误。在这种情况下,你不会准确地得到错误。

其次检查您是否在cordova文件中添加了权限。

提示:有些时候模拟器不能像真实设备那样提供完整的功能。也可以在真实设备中测试应用程序。