facebook

Write to a text file in internal storage of mobile device

  1. MobiOne Archive
  2.  > 
  3. Getting Help – General
Viewing 1 post (of 1 total)
  • Author
    Posts
  • #349580 Reply

    pratiti
    Member

    Hello,

    I need to create a text file in internal storage of mobile device and then write to it and save it via my code.

    I’m trying with this example from cordova docs, but this isn’t working:

    <script type="text/javascript" charset="utf-8">
        
        // Wait for device API libraries to load
        //
        document.addEventListener("deviceready", onDeviceReady, false);
    
        // device APIs are available
        //
        function onDeviceReady() {
            alert("ready");
            window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, gotFS, fail);
        }
    
        function gotFS(fileSystem) {
            alert("gotFs: " + fileSystem.name);
            fileSystem.root.getFile(example.txt, {create: true, exclusive: false}, gotFileEntry, fail);
        }
    
        function gotFileEntry(fileEntry) {
            alert("gotFileEntry: " + fileEntry.name);
            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) {
            alert("fail");
            console.log(error.code);
        }
        
        </script>

    This gives an alert of ‘success’ for writing, but when I run this on device, it gives ‘fail’ alert message. Also, I’m not clear about the location it uses to save the file created.

    Please help.
    Thanks in advance.

Viewing 1 post (of 1 total)
Reply To: Write to a text file in internal storage of mobile device

You must be logged in to post in the forum log in