简体   繁体   English

如何使用FileSystemObject在JavaScript中读取文件

[英]How to use FileSystemObject to read file in JavaScript

I want to read a file with FileSystemObject. 我想使用FileSystemObject读取文件。 My code is as following: 我的代码如下:

<!DOCTYPE html>
<html lang="en">
<head>
<title>Read json</title>
</head>

<body>
<script type="text/javascript">

function readFile(filename){
    var fso = new ActiveXObject("Scripting.FileSystemObject");
    var ForReading = 1;
    var f1 = fso.OpenTextFile(filename, ForReading);
    var text = f1.ReadAll();
    f1.close();
    return text;
}

myJSONText = "text.txt";
var myObject = readFile(myJSONText);//eval('(' + myJSONText + ')');

document.write(myObject.name);
</script>
</body>
</html>

First, let me repeat some comments above. 首先,让我重复上面的一些评论。 I've never seen using ActiveXObject client side extolled as a thing that should be done. 我从未见过使用ActiveXObject客户端作为应该完成的事情。

Now, let me say I'm trying to learn how to do this myself. 现在,让我说我正在尝试自己学习如何做。 Here are some thoughts (and helpful links, see the bottom) on this question. 这是有关此问题的一些想法(以及有用的链接,请参阅底部)。

The general layout, according to "Much ADO about Text Files" on MSDN's scripting clinic column , is: 根据MSDN的脚本诊所专栏上的“关于文本文件的ADO知识”,总体布局为:

  1. Create the object. 创建对象。
  2. Create another object, using the first, that uses a method of the first object (such as getting a file). 使用第一个创建另一个对象,该对象使用第一个对象的方法(例如获取文件)。
  3. Do things to the file. 对文件做事。
  4. Close the file. 关闭文件。

How do you start? 你如何开始? According to IE Dev Center (linked here ), use an ActiveXObject in Javascript as follows: 根据IE Dev Center( 在此处链接),在Javascript中使用ActiveXObject如下:

newObj = new ActiveXObject(servername.typename[, location])

You've got that when you declare fso in your code. 当您在代码中声明fso时,您已经知道了。 What about this "servername" thing, isn't the file accessed locally? 那“服务器名”呢,不是在本地访问文件吗? Instead of "servername etc" you've put in Scripting.FileSystemObject . 您已在Scripting.FileSystemObject了“ servername etc”,而不是“ servername etc”。 This is actually fine, if the HKEY_CLASSES_ROOT registry key on the host PC supports it (see ref above). 如果主机PC上的HKEY_CLASSES_ROOT注册表项支持它,则实际上是可以的(请参见上面的参考)。

Once the ActiveXObject is successfully declared, and if the browser allows it (IE only), and if the end user agrees to any warnings that pop up ("An ActiveX control on this page might be unsafe to interact with other parts of the page..." etc), then the object allows you to use any of the methods associated with that object. 一旦成功声明了ActiveXObject,并且在浏览器允许的情况下(仅限IE),并且最终用户同意弹出的任何警告(“此页面上的ActiveX控件与页面的其他部分进行交互可能是不安全的”)。 ..“等), 该对象允许您使用与该对象关联的任何方法。 That's where the power of the Windows Scripting FileSystemObject comes into play. 这就是Windows脚本FileSystemObject的功能发挥作用的地方。

Any FileSystemObject (fso) method is now available to use, which as its name suggests, means file (and directory) interaction on the local machine. 现在可以使用任何FileSystemObject(fso)方法,顾名思义,这意味着在本地计算机上进行文件(和目录)交互。 Not just reading, as your question is focused on, but writing and deleting as well. 不仅是您关注的重点是阅读,还包括写作删除 A complete list of methods and properties is available at MSDN here . 的方法和属性的完整列表,请访问MSDN 这里 After being used, close out the file using the .close() method. 使用完后,请使用.close()方法关闭文件。

So, this is dangerous for obvious reasons. 因此,出于显而易见的原因,这很危险。 But what wasn't obvious to me at first was that these interactions with the filesystem may happen invisibly . 但是起初对我而言并不明显的是,这些与文件系统的交互可能是看不见的 There is a good chance that whatever you do, from reading a file to deleting a directory tree, no warnings or command prompts will come up to let you know what's happening because of your few lines of code. 无论您做什么,从读取文件到删除目录树,都不会出现警告或命令提示的情况,这很可能使您知道由于几行代码而发生的事情。

Let me finish by commenting on the last bits of code above. 最后,让我对上面的代码最后几句进行评论。 Using JSON in conjunction with data pulled from the FileSystemObject provides a great way to allow JavaScript interaction ( JSON .parse and .stringify come immediately to mind). 将JSON与从FileSystemObject提取的数据结合使用,是一种允许JavaScript交互的好方法(立即想到JSON .parse和.stringify )。 With this, data could be stored locally, perhaps as an alternative to HTML5 local storage (ref this SO thread , which goes more in-depth with this concept, and another SO question I raised about this here ). 这样一来,数据就可以存储在本地,或者作为HTML5本地存储的替代(请参阅此SO线程 ,该线程对该概念进行了更深入的介绍,而我在此处提出了另一个SO问题)。

Here are some links for further reading: 以下是一些链接,供您进一步阅读:
IE Dev Center, JavaScript Objects, ActiveXObject IE开发人员中心,JavaScript对象,ActiveXObject
MSDN JScript Windows Scripting (including FileSystemObject methods, etc) MSDN JScript Windows脚本 (包括FileSystemObject方法等)
MSDN Scripting Clinic (older articles, many broken links, but stil a lot of good info on this stuff) MSDN脚本诊所 (较早的文章,许多断开的链接,但仍然有很多关于此方面的好信息)

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

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