简体   繁体   English

使用javascript读取本地计算机上的文件

[英]Reading a file on the local machine with javascript

I am new in javascript. 我是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. 我正在尝试做的是:编写一个脚本,该脚本将读取php或txt文件,获取信息(数字),然后像横幅广告一样将其替换在页面上。 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. 只有Internet Explorer通过ActiveXObject支持此功能。 You can access the file system using ActiveXObject and then read the file. 您可以使用ActiveXObject访问文件系统,然后读取文件。

See http://msdn.microsoft.com/en-us/library/2z9ffy99(v=vs.84).aspx 参见http://msdn.microsoft.com/zh-cn/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();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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