FileReader returns empty result for file from the bundle
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) {
c开发者_运维百科allback(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.
精彩评论