shiv
shiv

Reputation: 245

File reader not working PhoneGap Windows

I'm developing an Windows app on Visual Studio 2012 with cordova. I tried to create and then after read the content of a .txt file.

This is what I tried,

    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);
    } 

The file is created as the property onwrite is getting called but When I tried to see the content of file using alert alert(evt.target.result); then nothing happed.

Please help

Thanks

Upvotes: 0

Views: 148

Answers (1)

Ali
Ali

Reputation: 1122

You are using fail function in gotFileEntry,onDeviceReady and in gotFS functions. These functions perform requestFileSystem,getFile,and writeFile functionality. How would you debug that where is the error. So, write their separate fail handler function to easily go the error and then fix the error. In this case you will not accurately got the error.

secondly check that weather you have add the permission in cordova file or not.

Hint: some time emulator do not offer full functionality as real devices do. Also test the app in a real device.

Upvotes: 2

Related Questions