FileReader 从捆绑包中返回文件的空结果

发布于 2024-10-09 03:50:52 字数 551 浏览 0 评论 0原文

我尝试使用phonegap的FileReader类从应用程序包中读取文件:

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

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

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

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

在此示例中,路径类似于“index.html”。 onerror 回调永远不会被调用。 onload 被调用,但 evt.target.result 为空。您有什么建议吗?通常是否可以使用phonegap API 从捆绑包中读取文件?我可以使用像“./www/foo.txt”这样的相对路径吗?

感谢您的回答!

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

唱一曲作罢 2024-10-16 03:50:52

传递到 readAsText 的路径相对于应用程序沙箱中的“Documents”文件夹。因此,您必须通过替换该行来简单地修复路径

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

才能

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

访问该文件。这对我有用。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文