سؤال

I've tried to read a file from the app bundle using phonegap's FileReader class:

...
loadFile: function (path, callback) {
   fileReader = new FileReader(); 

   fileReader.onerror = function () {
     ...
   }

   fileReader.onload = function (evt) {
     callback(evt.target.result);
   }

   fileReader.readAsText("./www/" + path); 
}

In this example path is something like "index.html". The onerror callback is never called. onload is called but evt.target.result is empty. Do you have any suggestions? Is it in general possible to read files from the bundle with the phonegap API? Can I use relative paths like "./www/foo.txt"?

Thanks for your answers!

هل كانت مفيدة؟

المحلول

The path that is passed into readAsText is relative to the "Documents" folder in the applications sandbox. Hence you have to simply fix the path by replacing the line

fileReader.readAsText("./www/" + path); 

with

fileReader.readAsText("./../myApp.app/www/" + path); 

to access the file. This works for me.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top