javascript操作本地文件的代码分享

发布时间:2020-10-06编辑:脚本学堂
本文分享一例javascript操作本地文件的代码,学习下javascript中ActiveXObject等控件的用法,有需要的朋友参考下。

例子,javascript操作本地文件。
代码:
 

复制代码 代码示例:
<script type="text/javascript">
/**
* js代码操作本地文件
* by www.jb200.com
*/
function read(file) {
 if(typeof window.ActiveXObject != 'undefined') {
  var content = "";
  try {
   var fso = new ActiveXObject("Scripting.FileSystemObject"); 
   var reader = fso.openTextFile(file, 1);
   while(!reader.AtEndofStream) {
    content += reader.readline();
    content += "n";
   }
   // close the reader
   reader.close();
  }
  catch (e) {
   alert("Internet Explore read local file error: n" + e);
  }
  
  return content;
 }
    else if(document.implementation && document.implementation.createDocument) {
  var content = ""
  try {
   netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect');
   var lf = Components.classes["@mozilla.org/file/local;1"].createInstance(Components.interfaces.nsILocalFile);
   lf.initWithPath(file);
   if (lf.exists() == false) { 
                alert("File does not exist"); 
            }
   
            var fis = Components.classes["@mozilla.org/network/file-input-stream;1"].createInstance(Components.interfaces.nsIFileInputStream); 
            fis.init(lf, 0x01, 00004, null); 
            var sis = Components.classes["@mozilla.org/scriptableinputstream;1"].createInstance(Components.interfaces.nsIScriptableInputStream); 
            sis.init(fis); 
            var converter = Components.classes["@mozilla.org/intl/scriptableunicodeconverter"].createInstance(Components.interfaces.nsIScriptableUnicodeConverter); 
            converter.charset = "UTF-8"; 
            content = converter.ConvertToUnicode(sis.read(sis.available()));
  }
  catch (e) {
   alert("Mozilla Firefox read local file error: n" + e);
  }
  
  return content;
 }
}
</script>