日期:2014-05-16  浏览次数:20338 次

js 读取文件
script type="text/javascript"> 

02 function read(file) { 

03     if(typeof window.ActiveXObject != 'undefined') { 

04         var content = ""; 

05         try { 

06             var fso = new ActiveXObject("Scripting.FileSystemObject");   

07             var reader = fso.openTextFile(file, 1); 

08             while(!reader.AtEndofStream) { 

09                 content += reader.readline(); 

10                 content += "\n"; 

11             }  

12             // close the reader 

13             reader.close(); 

14         } 

15         catch (e) {  

16             alert("Internet Explore read local file error: \n" + e);  

17         } 

18           

19         return content; 

20     } 

21     else if(document.implementation && document.implementation.createDocument) { 

22         var content = ""

23         try { 

24             netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect'); 

25             var lf = Components.classes["@mozilla.org/file/local;1"].createInstance(Components.interfaces.nsILocalFile); 

26             lf.initWithPath(file); 

27             if (lf.exists() == false) {   

28                 alert("File does not exist");   

29             }  

30               

31             var fis = Components.classes["@mozilla.org/network/file-input-stream;1"].createInstance(Components.interfaces.nsIFileInputStream);   

32             fis.init(lf, 0x01, 00004, null);   

33             var sis = Components.classes["@mozilla.org/scriptableinputstream;1"].createInstance(Components.interfaces.nsIScriptableInputStream);   

34             sis.init(fis);   

35 &