上传文件框在firefox浏览器中显示路径不全的解决方法

记录下firefox浏览器中上传文件框显示路劲不全的解决办法。

html文本:

<div>

   <span style="font-size: 15px; margin-left:200px;">导入</span>
        <input type="file" name="picpath" id="picpath" style="display: none" onchange="picpathchange()"/>
        <input name="path" id="path" readonly="readonly" style = "350px;font-size: 15px;height: 35px;"  class="bigTextBox" />
        <input type="button" value="浏览..." onclick="picpathclick()" class="whiteBtn" />

</div>

js代码

<script type="text/javascript">

function picpathchange() {
        $("#path").html("");
        $("#path").val(FilePath.getFilePath(document.getElementById("picpath")));
    }
    function picpathclick() {
        $("#picpath").val("");
        $("#picpath").click();
    }
    var FilePath = {
        getFilePath: function (fileBrowser) {
            if (navigator.userAgent.indexOf("MSIE") != -1) { fileBrowser.select(); return document.selection.createRange().text; }
            else if (navigator.userAgent.indexOf("Firefox") != -1 || navigator.userAgent.indexOf("Mozilla") != -1) return this.getFilePathWithFF(fileBrowser);
            else alert("Not IE or Firefox (userAgent=" + navigator.userAgent + ")");
        },
        getFilePathWithFF: function (fileBrowser) {
            try {
                netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
            } catch (e) {
                alert('由于浏览器安全问题 请按照以下设置 [1] 地址栏输入 "about:config" ; [2] 右键 新建 -> 布尔值 ; [3] 输入 "signed.applets.codebase_principal_support" (忽略引号).');
                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;
                alert("File '" + fileName + "' cannot be loaded: relative paths are not allowed. Please provide an absolute path to this file.");
                return;
            }
            return file.path;
        }
    }

</script>

图示:

原文地址:https://www.cnblogs.com/Joans/p/2226516.html