简体   繁体   中英

Reading a file on the local machine with javascript

I am new in javascript. What I am trying to do is: write a script which will read an php or txt file, take the info (a number), and replace it on the page like a banner. It will be something like a rating, and the number of this rating will be taking on the local machine.

I need some script which will work with most of browsers.

Thank you for your help!!!

Only Internet Explorer supports this via ActiveXObject. You can access the file system using ActiveXObject and then read the file.

See http://msdn.microsoft.com/en-us/library/2z9ffy99(v=vs.84).aspx

Sample Code:

function ReadFiles()
{
   var fso, f1, ts, s;
   var ForReading = 1;
   fso = new ActiveXObject("Scripting.FileSystemObject");
   f1 = fso.CreateTextFile("c:\\testfile.txt", true);
   // Write a line.
   Response.Write("Writing file <br>");
   f1.WriteLine("Hello World");
   f1.WriteBlankLines(1);
   f1.Close();
   // Read the contents of the file.
   Response.Write("Reading file <br>");
   ts = fso.OpenTextFile("c:\\testfile.txt", ForReading);
   s = ts.ReadLine();
   Response.Write("File contents = '" + s + "'");
   ts.Close();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM