读取JavaScript中的文本文件 [英] Read text file in JavaScript

查看:89
本文介绍了读取JavaScript中的文本文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

如何使用JavaScript读取文本文件?

以下代码对我来说很好用

Hi All,

How to read a text file in JavaScript?

following code is working fine for me

var fso = new ActiveXObject("Scripting.FileSystemObject");
var tempFile = fso.OpenTextFile(fname, 1);
var line = tempFile.ReadLine();



但显示的是一条安全消息,

此页面上的ActiveX控件与页面的其他部分进行交互可能不安全.您要允许这种互动吗?
[是] [否]

以下代码也不起作用



but it is showing a security message that

An ActiveX control on this page might be unsafe to interact with other parts of the page. Do you want to allow this interaction?
[Yes] [No]

Following code is also not working

var text = '''';
var filechar;
netscape.security.PrivilegeManager.enablePrivilege(''UniversalFileAccess''); 
var file = new java.io.File(fname);
var FileReader = new java.io.FileReader(file);
filechar = FileReader.read();
while (filechar != -1) {
text = text + String.fromCharCode(filechar);
filechar = FileReader.read();
}
FileReader.close();
alert(text);



它在第3行上显示以下错误.
Microsoft JScript运行时错误:"netscape"未定义

任何人都可以毫无问题地找到有效的解决方案吗?

在此先感谢
-Amol



it is showing following error on line number 3.
Microsoft JScript runtime error: ''netscape'' is undefined

Can anyone has a working solution without any problem?

Thanks in advance
-Amol

推荐答案

请参见 ^ ]的原因,您将始终需要用户权限.

干杯,
彼得
See this thread[^] for the reason why you will always require user permission.

Cheers,
Peter


这篇关于读取JavaScript中的文本文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆