日期:2014-05-20  浏览次数:20743 次

高分求救,firefox,jsp文件上传
纯jsp项目,无servlet
在firefox浏览器里如何获得上传文件路径。。。
求代码。。。能用的

------解决方案--------------------
探讨

<input type="file" id="file"/>
var 文件路径 = document.getElementById("file").value

------解决方案--------------------
一般的真不行还是你代码有误,说实话以前还真没注意过。莫非firefox还不支持html的file标签了....
------解决方案--------------------
FF3现在好像是不支持了。 要改为通用的有些麻烦。
在网上找了一个。可用
HTML code

<!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>