用cordova轻松读取WWW文件夹中的文件

easily read file in the www folder with cordova

本文关键字:文件夹 文件 WWW 读取 cordova      更新时间:2023-09-26

我发现了很多关于如何阅读位于www文件夹中的简单文件的主题。它们中的大多数都是旧的,或者没有给出如何使用它的例子。我尝试了cordova文件插件

cordova plugin add org.apache.cordova.file

但是我得到这个错误:

Error: Registry returned 404 for GET on https://registry.npmjs.org/org.apache.cordova.file

我试图使用XMLhttpRequest解决方案,但在模拟器中我没有任何结果。下面是我的代码:

function xmlLoadFile()
{
var xmlhttp;
if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  }
else
  {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
xmlhttp.onreadystatechange=function()
  {
  if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
    document.body.innerHTML=xmlhttp.responseText;
    }
}
xmlhttp.open("GET","test.txt",true);
xmlhttp.send();
}

我有一个在我的index.html,所以我不知道为什么我仍然有一个空白页,当我运行我的应用程序。

提前感谢您的回答。

应该是

cordova plugin add cordova-plugin-file

请参考此网址

https://github.com/apache/cordova-plugin-file

请使用任何其他插件,如果你正在使用ajax。您可以使用jQuery或类似的插件,否则您将最终编写大量代码。