最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

How to use FileSystemObject to read file in JavaScript - Stack Overflow

programmeradmin3浏览0评论

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>

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>
Share Improve this question edited Jan 28, 2014 at 20:22 tshepang 12.5k25 gold badges97 silver badges139 bronze badges asked Oct 17, 2012 at 6:06 BryantBryant 3493 gold badges6 silver badges11 bronze badges 2
  • Gah... Okay, first that code looks fine, as long as you're in IE, so I'm not sure what's wrong with it. BUT do not do this! You're trying to ready a local file system from a client browser, which almost every browser doesn't allow. And even though IE (older versions only?) allow it like this, it's still a security issue. If you want to read a file you should be prompting the user to upload it and then reading it on the server (or reading it on the client using new HTML5 file APIs) – user578895 Commented Oct 17, 2012 at 6:11
  • OK, Thanks for your help. Now, let me have a try. Many thanks again. – Bryant Commented Oct 17, 2012 at 6:15
Add a ment  | 

1 Answer 1

Reset to default 6

First, let me repeat some ments 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 es 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 plete 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 mand prompts will e up to let you know what's happening because of your few lines of code.

Let me finish by menting 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 e 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)

发布评论

评论列表(0)

  1. 暂无评论