日期:2014-05-20 浏览次数:20933 次
<html>
<head>
<title>JS获取上传文件的绝对路径,兼容IE和FF</title>
<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('路径错误!');
return;
}
var fileName=fileBrowser.value;
var file = Components.classes["@mozilla.org/file/local;1"]
.createInstance(Components.interfaces.nsILocalFile);
try {
file.initWithPath( fileName.replace(/\//g, "\\\\") );
}
catch(e) {
if (e.result!=Components.results.NS_ERROR_FILE_UNRECOGNIZED_PATH) throw e;
return;
}
if ( file.exists() == false ) {
alert("File '" + fileName + "' not found.");
return;
}
alert(file.path);
}
function readFileIE(fileBrowser) {
alert(document.getElementById("fileBrowser").value);
}
</script>
</head>
<body>
<form name="form1">
<input type="file" name="fileBrowser" size="50" onchange="readFile(this)" />
</form>
</body>
</html>