简体   繁体   English

用Javascript读取文本文件?

[英]Read text file with Javascript?

I'm working on mobile app through XDK intel, How can I read text file from outside the app?我正在通过 XDK intel 开发移动应用程序,如何从应用程序外部读取文本文件?

I tried this code, but it's not working!我试过这段代码,但它不起作用!

 function readFile (filepath){ var txtFile = "C:\\test.txt"; var file = new File(txtFile); file.open("r"); // open file with read access var str = ""; while (!file.eof) { // read each line of text str += file.readln() + "\\n"; } file.close(); alert(str); }

If you are using Intel XDK try the Cordova-Plugin-File: https://github.com/apache/cordova-plugin-file.git . 如果您使用的是Intel XDK,请尝试Cordova-Plugin-File: https : //github.com/apache/cordova-plugin-file.git Is a core plugin for write and read files. 是用于写入和读取文件的核心插件。 hope thats helps you. 希望那对您有帮助。

You are building a Cordova app (aka PhoneGap app) when you are creating an app for the Intel XDK. 在为英特尔XDK创建应用程序时,您正在构建Cordova应用程序(又名PhoneGap应用程序)。 So the rules (and solutions) that apply to Cordova also apply to Intel XDK apps. 因此,适用于Cordova的规则(和解决方案)也适用于Intel XDK应用。 The XDK is simply providing a way to make it easier to apply and use the standard Cordova tools, it is not providing a different runtime environment (other than including the option to replace the Android webview with a Crosswalk webview). XDK只是提供一种简化应用和使用标准Cordova工具的方法,它没有提供不同的运行时环境(除了提供了用Crosswalk Webview替换Android Webview的选项之外)。

This describes, at a very high level, what Cordova and Cordova plugins do on an Android device > http://developer.android.com/guide/webapps/webview.html . 这在很高的层次上描述了Cordova和Cordova插件在Android设备上的作用> http://developer.android.com/guide/webapps/webview.html Similar approaches are taken on iOS and Windows and other platforms supported by Cordova. 在iOS和Windows以及Cordova支持的其他平台上也采用了类似的方法。

As Nicolas stated in his answer, you should use the Cordova File Plugin . 正如Nicolas在他的回答中所述,您应该使用Cordova File Plugin On the doc page for that plugin you'll find two references that are worth reading: 在该插件的文档页面上,您会找到两个值得阅读的参考:

Also, these may be of value: 同样,这些可能很有价值:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM