<script type="text/javascript"> function readFile(fileBrowser) { if (navigator.userAgent.indexOf("MSIE")!=-1) readFileIE(fileBrowser); else if (navigator.userAgent.indexOf("Firefox")!=-1 || navigator.userAgent.indexOf("Mozilla")!=-1) readFileFirefox(fileBrowser); else alert("Not IE or Firefox (userAgent=" + navigator.userAgent + ")"); }
function readFileFirefox(fileBrowser) { try { netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect"); } catch (e) { alert('Unable to access local files due to browser security settings. To overcome this, follow these steps: (1) Enter "about:config" in the URL field; (2) Right click and select New->Boolean; (3) Enter "signed.applets.codebase_principal_support" (without the quotes) as a new preference name; (4) Click OK and try loading the file again.'); return; }
var fileName=fileBrowser.value; var file = Components.classes["@mozilla.org/file/local;1"] .createInstance(Components.interfaces.nsILocalFile); try { // Back slashes for windows file.initWithPath( fileName.replace(/\//g, "\\") ); } catch(e) { if (e.result!=Components.results.NS_ERROR_FILE_UNRECOGNIZED_PATH) throw e; alert("File '" + fileName + "' cannot be loaded: relative paths are not allowed. Please provide an absolute path to this file."); return; }
if ( file.exists() == false ) { alert("File '" + fileName + "' not found."); return; } alert(file.path); // I test to get the local file's path. var is = Components.classes["@mozilla.org/network/file-input-stream;1"].createInstance( Components.interfaces.nsIFileInputStream ); try { is.init( file,0x01, 00004, null); } catch (e) { if (e.result!=Components.results.NS_ERROR_FILE_ACCESS_DENIED) throw e; alert("Unable to access local file '" + fileName + "' because of file permissions. Make sure the file and/or parent directories are readable."); return; } var sis = Components.classes["@mozilla.org/scriptableinputstream;1"].createInstance( Components.interfaces.nsIScriptableInputStream ); sis.init( is ); var data = sis.read( sis.available() ); var s = file.path; //文件路径 //s += "建立时间: " + Date.parse(file.DateCreated) + " "; //s += "最后访问时间: " + Date.parse(file.DateLastAccessed) + " "; //s += "最后修改时间: " + Date.parse(file.DateLastModified) ; alert("Data from file: " + data); // I test to get the local file's data. }
function readFileIE(fileBrowser) { var data ,f,s; try { var fso = new ActiveXObject("Scripting.FileSystemObject");
var fileName=fso.GetAbsolutePathName(fileBrowser.value);
if (!fso.FileExists(fileName)) { alert("File '" + fileName + "' not found."); return; }
f = fso.GetFile(fileName); // filespec 是指定文件的路径(绝对和或相对的),必选项。 s = f.Path.toUpperCase() + "<br>"; //文件路径 s += "建立时间: " + Date.parse(f.DateCreated) + " "; s += "最后访问时间: " + Date.parse(f.DateLastAccessed) + " "; s += "最后修改时间: " + Date.parse(f.DateLastModified) ;
// var file = fso.OpenTextFile(fileName, 1);
//data = file.ReadAll(); alert("Data from file: " + s); f.Close(); } catch(e) { if (e.number == -2146827859) { // This is what we get if the browser's security settings forbid // the use of the FileSystemObject ActiveX control alert('Unable to access local files due to browser security settings. To overcome this, go to Tools->Internet Options->Security->Custom Level. Find the setting for "Initialize and script ActiveX controls not marked as safe" and change it to "Enable" or "Prompt"'); } else if (e.number == -2146828218) { // This is what we get if the browser can't access the file // because of file permissions alert("Unable to access local file '" + fileName + "' because of file permissions. Make sure the file and/or parent directories are readable."); } else throw e; } } </script>
</head> <body> <form name="form1"> Browse to select a file <input type="file" name="fileBrowser" size="125" onchange="readFile(this)" /> </form> </body> </html>