开发者

FileReader returns empty result for file from the bundle

开发者 https://www.devze.com 2023-02-01 18:37 出处:网络
I\'ve tried to read a file from the app bundle using phonegap\'s FileReader class: ... loadFile: function (path, callback) {

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消