日期:2014-05-20 浏览次数:20743 次
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=gbk" /> <title>Untitled Page</title> <script type="text/javascript"> function readFile(fileBrowser) { if (navigator.userAgent.indexOf("MSIE")!=-1){ alert(document.getElementById("filePath").value); } else if (navigator.userAgent.indexOf("Firefox")!=-1 || navigator.userAgent.indexOf("Mozilla")!=-1) alert(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 { var temp = ///g; file.initWithPath(fileName.replace(temp,"//")); } 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; } return file.path; } </script> </head> <body> <form name="form1"> FireFox中得到file控件取到客户端文件的完整路径 <input id="filePath" type="file" name="fileBrowser" size="125" onchange="readFile(this)"/> </form> </body> </html>