Recently, I encountered some problems in getting the file path because I needed to upload files to oracle blob. Due to security reasons, the new browser does not support directly obtaining local URLs. I have found some methods online, as follows:
<script type="text/javascript">//FX get file path method function readFileFirefox(fileBrowser) { try { netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect"); } catch (e) { alert('The local file cannot be accessed due to browser security settings. To overcome this, please follow the following steps: (1) Enter "about:config" in the address bar; (2) Right-click and select New->Boolean; (3) Enter "signed.applets.codebase_principal_support" (excluding quotes) as the name of a new preference; (4) Click OK and try to reload the file'); return; } var fileName=fileBrowser.value; //This step can get the complete path of the client. Whether the judgment below is too complicated, and the judgment below is also very complicated. 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; } return file.path;}//Get path according to different browsers function getvl(obj){//Judge browser var Sys = {}; var ua = navigator.userAgent.toLowerCase(); var s; (s = ua.match(/msie ([/d.]+)/)) ? Sys.ie = s[1] : (s = ua.match(/firefox//([/d.]+)/)) ? Sys.firefox = s[1] : (s = ua.match(/chrome//([/d.]+)/)) ? Sys.chrome = s[1] : (s = ua.match(/opera.([/d.]+)/)) ? Sys.opera = s[1] : (s = ua.match(/version//([/d.]+).*safari/)) ? Sys.safari = s[1] : 0; var file_url=""; if(Sys.ie<="6.0"){ //ie5.5,ie6.0 file_url = obj.value; }else if(Sys.ie>="7.0"){ //ie7,ie8 obj.select(); file_url = document.selection.createRange().text; }else if(Sys.firefox){ //fx //file_url = document.getElementById("file").files[0].getAsDataURL();//The path obtained is the encrypted string recognized by FF file_url = readFileFirefox(obj); }else if(Sys.chrome){ file_url = obj.value; } //alert(file_url); document.getElementById("text").innerHTML="Get the complete path of the file domain as: "+file_url;}</script><h1>JS method to get the complete path of the file domain, compatible with different browsers</h1><div id="text" style="color:#f00;"></div><input type="file" id="file" onchange="getvl(this)" />The above code is used normally in IE 6 7 8. Under IE9, document.selection.createRange() denies access, which seems to be improved.
Finally, the test found that in IE9, if the file control gets focus, document.selection.createRange() denies access.
Therefore, you only need to add a sentence obj.blur() after obj.select().
EX:
else if(Sys.ie>="7.0"){ //ie7,ie8 obj.select(); obj.blur(); file_url = document.selection.createRange().text; }// obj = document.getElementById("file");The above method (recommended) for obtaining the absolute path of input file in JS is all the content shared by the editor. I hope it can give you a reference and I hope you can support Wulin.com more.