reading a txt file using html/javascript

hello  every one
I one of my application i want to display the txt file content into textbox
. So how can i do it
i search in internet i got this code
I took a script tag and place this code

function read()
//  var   file = fopen(getScriptPath("a.txt"), 0);
//  var   file_length = flength(file);
//  var   content = fread(file, file_length);
//document.getElementById("myDiv").innerText = content;
        var fso  = new ActiveXObject(‘Scripting.FileSystemObject’);
        var fh = fso.OpenTextFile(‘a.txt’,1);
        var contents = fh.ReadAll();
        //return contents;
    catch (Exception)
       alert(‘Cannot open file :(‘);


the commented one is also not working nor the other one so can anyone guide me or give some suggestions.

check out below link,

you can’t use javascript because it will run on the client machine..


You can’t read Files using JavaScript due to security reasons!

You have used ActiveX, but it won’t work other than IE browsers!

You can use Server Side Code(C#/VB) to read the file and update it to the textbox!

Hope it helps u…


check my blog post for reading clientside files.

check reference sites there
to read from txt files.

Leave a Reply