Software Archive
Read-only legacy content

Issue with file plugin

Antonio_A_
Beginner
305 Views

Hi all, I'm trying to use the file plugin in a project, in order to read from an existing .txt file (filling some form fields) and to overwrite it with new information, taking data from the fields after user's editing.

Well, I've managed to achieve the first step (reading), but not to write. Despite the "ok" feedback from the writing function, when I try to read the .txt file, I see the information inside haven't changed.

Below is the writing function I'm using. Can anybody help me to understand? 

Thank you very much.

Antonio

 

var salvaImpostazioni= function() {
                requestFileSystem(LocalFileSystem.PERSISTENT, 0, onSuccessWrite, onError);
            }

            function onSuccessWrite(fileSystem) 
            {   
                var directoryEntry = fileSystem.root;
                //lets create a file named readme.txt. getFile method actually creates a file and returns a pointer(FileEntry) if it doesn't exist otherwise just returns a pointer to it. It returns the file pointer as callback parameter.
                directoryEntry.getFile("settings.txt", {create: true, exclusive: false}, function(fileEntry){
                    //lets write something into the file
                    fileEntry.createWriter(function(writer){
                        content= document.getElementById("demo");
                        writer.write(content);
                        alert("OK");
                        //document.getElementById("errore").innerHTML= "ok";
                        //requestFileSystem(LocalFileSystem.PERSISTENT, 0, leggiSuccess, onError);
                        //document.addEventListener("deviceready", leggiSuccess, false);
                    }, function(error){
                        
                        console.log("1.Error occurred while writing to file. Error code is: " + error.code);
                        document.getElementById("errore").innerHTML= error.code;
                    });
                }, function(error){
                   
                    console.log("2.Error occurred while getting a pointer to file. Error code is: " + error.code);
                    document.getElementById("errore").innerHTML= error.code;
                });
            }

            function onError(evt)
            {
                content= document.getElementById("demo");
                alert(content);    
                //alert("LL");   
                console.log("3.Error occurred during request to file system pointer. Error code is: " + evt.code);
                document.getElementById("errore").innerHTML= evt.code;
            }

0 Kudos
3 Replies
PaulF_IntelCorp
Employee
305 Views

I recommend you read the file plugin docs, especially this section > https://github.com/apache/cordova-plugin-file#where-to-store-files < and this HTML5 Rocks tutorial > http://www.html5rocks.com/en/tutorials/file/filesystem/ < You cannot write to just any location on your device, including the app location (see the plugin docs). Also, you cannot treat a mobile device filesystem like you would your PC filesystem, so what you can do in a browser may not work on a device.

0 Kudos
Antonio_A_
Beginner
305 Views

Ok, I already read these docs, but still I can't find what's wrong in my code. It's even more difficult to find errors, because the app looks like to work properly on the device, since I receive the "ok alert". In simulation mode the app's behaviour is the same, so I don't know what to do.

 

0 Kudos
Diego_Calp
Valued Contributor I
305 Views

Hi Antonio,

If you need to save and read small chunks of data, why don't you use HTML5 web storage options? It uses key-value format, so it will also be easier to save and retrieve values than parsing a txt.

Here is a good tutorial.

https://www.sitepoint.com/html5-web-storage/

I used it a lot with XDK, works very well.

Regards

Diego

0 Kudos
Reply