October 19, 2000 - Constructing A File Object | WebReference

October 19, 2000 - Constructing A File Object

Yehuda Shiran October 19, 2000
Constructing A File Object
Tips: October 2000

Yehuda Shiran, Ph.D.
Doc JavaScript

One of the less-covered subjects in JavaScript is the File IO. Usually, a JavaScript script operates on the HTML code that is already loaded onto the page, and it does not need to get more information from an external file. Like many other applications, the File IO is also based on the ActiveX technology. You create an ActiveX File object by calling ActiveXObject() with a single argument, Scripting.FileSystemObject:

myActiveXObject = new ActiveXObject("Scripting.FileSystemObject");

You can create a File object by using the ActiveX's GetFile() method:

file = myActiveXObject.GetFile("c:\\command.com");

The File object supports various properties and methods. The following code shows how to use the name property:

alert("The name of the file is: " + file.name);

We don't show a live demo here, as for security reasons you cannot access the client file system from a server-based file.

Learn more about ActiveX controls in Column 55, OLE Automation in JavaScript.