Page 1 of 2

write/read a file?

Posted: Sat Oct 24, 2015 2:49 am
by Elvin Chu

I tried this, it didn't work. Another question how can I read a file with path.
code
function saveTheFile(filename, content) {
window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, function(fs) {
fs.root.getFile(filename, { create: true }, function (fileEntry) {

Code: Select all

                 fileEntry.createWriter(function (fileWriter) { 

    fileWriter.onwriteend = function (e) { 

                     }; 

  fileWriter.onerror = function (e) { 
                         alert('Error occured: ' + e.toString() + "\n File couldn't saved"); 
                     }; 

                     var contentBlob = new Blob([content], { type: 'text/plain' }); 

                     fileWriter.write(contentBlob); 

                 }, handleError); 

             }, handleError); 
         } 
     ); 

}
saveTheFile(bg.txt,"aaaaa");
/code


write/read a file?

Posted: Sat Oct 24, 2015 2:58 am
by Elvin Chu

I fixed using

code
window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, function(fs) {

fs.root.getFile("GREN/background.css",{create: true}, function(DatFile){

DatFile.createWriter(function(DatContent) {

var blob = new Blob(["Lorem Ipsum"],{type: "text/plain"});

DatContent.write(blob);

Code: Select all

 }); 

});

Code: Select all

         } 
     ); 

/code


write/read a file?

Posted: Sat Oct 24, 2015 3:21 am
by Elvin Chu

How to read a file?


write/read a file?

Posted: Sat Oct 24, 2015 12:15 pm
by Illya Stepanov

Hi Elvin -

This question relates to Cordova APIs the best place is to check their documentation on git: https://github.com/apache/cordova-plu...

-- and also here is a good article: http://www.html5rocks.com/en/tutorial...


write/read a file?

Posted: Sat Oct 24, 2015 12:17 pm
by Elvin Chu

Hello I read already :) but the code I use it didn't work at all can u help me fix it please?

code
window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, function(fs) {

fs.root.getFile('GREN/background.css', {}, function(fileEntry) {

Code: Select all

 fileEntry.file(function(file) { 
    var reader = new FileReader(); 

    reader.onloadend = function(e) { 

     $scope.file = this.result; 

    }; 

    reader.readAsText(file); 
 }, errorHandler); 

}, errorHandler);
});
/code


write/read a file?

Posted: Sat Oct 24, 2015 12:43 pm
by Illya Stepanov

It's hard to say for sure without knowing your code context here.

Could you explain with more details what you are trying to implement in application, what builder version you are using and on what device platform?


write/read a file?

Posted: Sat Oct 24, 2015 12:46 pm
by Elvin Chu

I tested on my phone with android,5.0


write/read a file?

Posted: Sat Oct 24, 2015 12:46 pm
by Elvin Chu

It didn't work at all


write/read a file?

Posted: Sat Oct 24, 2015 12:51 pm
by Illya Stepanov

Where exactly in your app you using this code?


write/read a file?

Posted: Sat Oct 24, 2015 1:00 pm
by Elvin Chu