1

Our code always goes to fail section. However, we have changed path several time like

'file:///android_asset/www/readme.txt' ,
'../android_asset/www/readme.txt', 
'/www/readme.txt", "readme.txt'.
[We have taken "readme.txt"  file in www folder]

We picked up the code from the below link.

http://docs.phonegap.com/phonegap_file_file.md.html

2 Answers2

0
 <!DOCTYPE html>
 <html>
 <head>
  <title>FileReader Example</title>

<script type="text/javascript" charset="utf-8" src="cordova-1.5.0.js"></script>
<script type="text/javascript" charset="utf-8">

// Wait for PhoneGap to load
//
function onLoad() {
    document.addEventListener("deviceready", onDeviceReady, false);
}

// PhoneGap is ready
//
function onDeviceReady() {
    window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, gotFS, fail);
}

function gotFS(fileSystem) {
    fileSystem.root.getFile("file:///sdcard/example.txt", {create: true}, gotFileEntry, fail);
}

function gotFileEntry(fileEntry) {
    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) {
    var reader = new FileReader();
    reader.onloadend = function(evt) {
        console.log("Read as text");
        console.log(evt.target.result);
    };
    reader.readAsText(file);
}

function fail(evt) {
    console.log(evt.target.error.code);
}

</script>

Andrew Barber
  • 39,603
  • 20
  • 94
  • 123
Shyantanu
  • 681
  • 1
  • 12
  • 30
0

I asked a similar question and couldn't really find a solution. Here's a complete example of our method call:

window.resolveLocalFileSystemURI("file:///android_asset",
  function(entry){
    console.log(entry.fullPath);},
  function(evt){
    console.log(evt.code);}
);

However during the first phase we've only got a undefined error code while in a fresh testing project we receive error code 1 (file not found, line 56).

By the way: did you realised you miss a backslash? Try to reference file:///android_asset and if it works let me know what you did :)

Community
  • 1
  • 1
nuala
  • 2,681
  • 4
  • 30
  • 50