导航栏: 首页 评论列表

使用javascript读取本地文件

默认分类 2011-12-05 02:02:07

有时候我们需要读取本地文件,下面是使用非安全脚本读取本地txt文件的方法:

  1. <script type="text/javascript">    
  2. function read(file) {    
  3.      if(typeof window.ActiveXObject != 'undefined') {    
  4.          var content = "";    
  5.          try {    
  6.              var fso = new ActiveXObject("Scripting.FileSystemObject");      
  7.              var reader = fso.openTextFile(file, 1);    
  8.              while(!reader.AtEndofStream) {    
  9.                  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.          return content;    
  19.      }    
  20.      else if(document.implementation && document.implementation.createDocument) {    
  21.          var content = ""  
  22.          try {    
  23.              netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect');    
  24.              var lf = Components.classes["@mozilla.org/file/local;1"].createInstance(Components.interfaces.nsILocalFile);    
  25.              lf.initWithPath(file);    
  26.              if (lf.exists() == false) {      
  27.                  alert("File does not exist");      
  28.              }    
  29.                   
  30.              var fis = Components.classes["@mozilla.org/network/file-input-stream;1"].createInstance(Components.interfaces.nsIFileInputStream);      
  31.              fis.init(lf, 0x0100004null);      
  32.              var sis = Components.classes["@mozilla.org/scriptableinputstream;1"].createInstance(Components.interfaces.nsIScriptableInputStream);      
  33.              sis.init(fis);      
  34.              var converter = Components.classes["@mozilla.org/intl/scriptableunicodeconverter"].createInstance(Components.interfaces.nsIScriptableUnicodeConverter);      
  35.              converter.charset = "UTF-8";      
  36.              content = converter.ConvertToUnicode(sis.read(sis.available()));    
  37.          }    
  38.          catch (e) {    
  39.              alert("Mozilla Firefox read local file error: \n" + e);    
  40.          }    
  41.             
  42.          return content;    
  43.      }    
  44. }    
  45. </script>   

安全的方法是使用xmlhttp读取本地html文件:

  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
  2. <html xmlns="http://www.w3.org/1999/xhtml">  
  3.   
  4. <head>  
  5. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />  
  6. <title>Untitled 1</title>  
  7. <script type="text/javascript">  
  8. <!--  
  9. function doit(){  
  10.     //todo  
  11.     var xmlhttp=null;  
  12.     if(window.ActiveXObject){  
  13.         xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");  
  14.     }else if (window.XMLHttpRequest){// code for all new browsers  
  15.         xmlhttp=new XMLHttpRequest();  
  16.     }  
  17.     if(xmlhttp!=null){  
  18.       xmlhttp.open("GET","book.txt",false);    
  19.       xmlhttp.send();  
  20.       alert(xmlhttp.responseText);    
  21.     }else {  
  22.       alert("Your browser does not support XMLHTTP.");  
  23.     }  
  24.   
  25.       
  26. }  
  27.   
  28. //-->  
  29. </script>  
  30. </head>  
  31.   
  32. <body><button type="button" onclick="doit()">doit</button>  
  33.   
  34. </body>  
  35.   
  36. </html>  


>> 留言评论