Gnostice Document Studio
(for Java)
(for Java)
Gnostice Document Studio
Web APIs

Gnostice Blog

Friday, December 11, 2015

How to read local files from a web page using JavaScript

As the Scripting Guy[1] once said, "Hey, Internet Explorer can do anything!"

By V. Subhash

When creating web pages in a text editor, you need to open a separate window for the browser to display the page? You also need to refresh that page every now and then. How about editing the file and viewing the rendered HTML right in the browser itself?

More than a decade ago, I had an ActiveX-based HTML web page text editor. When I tried to re-create that code in response to a StackOverFlow question, I discovered that ActiveX still works in the browser, albeit with a prompt. Back then, the HTML page could be added to "Trusted Sites" zone and there would be no more prompts. Alas, not so, anymore!

Here is the code to get you started on a journey to certain doom;-)

function readZeFile() {
try {
var oField = document.getElementById("z_file");
var oFS = new ActiveXObject("Scripting.FileSystemObject");
var oStream = oFS.OpenTextFile(oField.value, 1);
var sContents = oStream.readAll();
document.getElementById("BoxFileContents").innerHTML = sContents;
} catch (e) {

<input type="file" name="z_file" id="z_file" size="50" />
<input type="button" onclick="readZeFile()" value="Read ze file" />

<pre id="BoxFileContents" style="border: 1px dotted black; height: 10em; width: 500; overflow: scroll; ">
[Nothing loaded]


Just so that you don't get too many ideas based on this legacy feature, here is the obligatory link to my Limitations of Web Applications article.

[1] - Hey, Scripting Guy! Blog; How Can I Grab a URL From the Clipboard and Then Open That Web Site in a Browser?
Privacy | Legal | Feedback | Newsletter | Blog | Resellers © 2002-2019 Gnostice Information Technologies Private Limited. All rights reserved.