如何使用FileSystemObject在JavaScript中读取文件 [英] How to use FileSystemObject to read file in JavaScript

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

问题描述

我想用FileSystemObject读取一个文件。我的代码如下:

 <!DOCTYPE html> 
< html lang =en>
< head>
< title>读取json< / title>
< / head>

< body>
< script type =text / javascript>

函数readFile(filename){
var fso = new ActiveXObject(Scripting.FileSystemObject);
var ForReading = 1;
var f1 = fso.OpenTextFile(filename,ForReading);
var text = f1.ReadAll();
f1.close();
返回文本;
}

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

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


解决方案

首先,让我重复上面的一些评论。 我从来没有见过使用ActiveXObject客户端作为应该完成的事情。



现在,让我说我正在努力学习如何自己做这个。下面是关于这个问题的一些想法(和有用的链接,请参阅底部)。



根据MSDN上的关于文本文件的很多ADO =http://msdn.microsoft.com/en-us/library/ms974559.aspx =nofollow noreferrer>脚本诊所专栏,是:


  1. 创建对象。
  2. 创建另一个对象,使用第一个对象,使用第一个对象的
    a方法(例如获取文件)。

  3. 将文件做成

  4. 关闭文件。 >

    你如何开始?根据IE开发中心(链接这里在Javascript中使用ActiveXObject,如下所示:

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

    当您声明 fso 在你的代码中。那么这个服务器名称的东西,不是本地访问的文件?而不是servername etc,你已经把 Scripting.FileSystemObject 。实际上,如果主机上的HKEY_CLASSES_ROOT注册表项支持它(见上面的参考文献)。
    $ b 一旦ActiveXObject被成功声明,并且浏览器允许(仅IE),如果最终用户同意弹出任何警告(此页上的ActiveX控件可能不安全与页面的其他部分交互...等),则然后该对象允许您使用与该对象关联的任何方法。这就是Windows Scripting FileSystemObject的强大功能。



    现在可以使用任何FileSystemObject(fso)方法,正如其名称所暗示的那样,目录)在本地机器上的交互。不仅仅是阅读,而是关注你的问题,而且 删除。方法和属性的完整列表可以在MSDN上找到这里。使用后,使用 .close()方法关闭文件。

    所以,这对于明显的原因。但起初我不明白的是,这些与文件系统的交互可能会无形地发生。无论你做什么,从阅读文件到删除目录树,都不会出现任何警告或命令提示,因为你只需要几行代码就可以知道发生了什么。



    让我完成对上面代码的最后几位的评论。将JSON与从FileSystemObject提取的数据结合使用,提供了一种允许JavaScript交互的好方法(立刻就来了)。有了这个,数据可以存储在本地,也许作为HTML5本地存储的替代(参考这个线程,更多深入了解这个概念,以及我提出的关于这个这里的另一个问题。)



    以下是一些进一步阅读的链接:

    IE开发人员中心,JavaScript对象,ActiveXObject

    MSDN JScript Windows脚本(包括FileSystemObject方法等)

    MSDN Scripting Clinic (旧的文章,很多断开的链接,但是关于这个东西很多很好的信息)

    I want to read a file with 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.

    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:

    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:

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

    You've got that when you declare fso in your code. What about this "servername" thing, isn't the file accessed locally? Instead of "servername etc" you've put in Scripting.FileSystemObject. This is actually fine, if the HKEY_CLASSES_ROOT registry key on the host PC supports it (see ref above).

    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. That's where the power of the Windows Scripting FileSystemObject comes into play.

    Any FileSystemObject (fso) method is now available to use, which as its name suggests, means file (and directory) interaction on the local machine. 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. After being used, close out the file using the .close() method.

    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). 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).

    Here are some links for further reading:
    IE Dev Center, JavaScript Objects, ActiveXObject
    MSDN JScript Windows Scripting (including FileSystemObject methods, etc)
    MSDN Scripting Clinic (older articles, many broken links, but stil a lot of good info on this stuff)

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

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