Home>

Recently, I needed to upload a file to the oracle blob. I encountered some problems in obtaining the file path.For security reasons,The new version of the browser does not support directly obtaining the local URL. I found some methods on the Internet.as follows:

<script type="text/javascript">
//fx get file path method
function readfilefirefox (filebrowser) {
  try {
    netscape.security.privilegemanager.enableprivilege ("universalxpconnect");
  }
  catch (e) {
    alert ("Unable to access local file,Due to browser security settings.
To overcome this,Please follow the steps below:(1) Enter "about:config" in the address bar;(2) Right-click and select new->boolean;(3) Enter "signed.applets.codebase_principal_support" (without quotes) as a The name of the new preference;
(4) Click ok and try to reload the file ");
    return;
  }
  var filename=filebrowser.value;//This step will get the full path of the client.
The following judgment is too complicated,And the following get IE 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 the path according to different browsers
function getvl (obj) {
//Judge the 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 obtained path is an encrypted string identified by ff
  file_url=readfilefirefox (obj);
 } else if (sys.chrome) {
  file_url=obj.value;
 }
 //alert (file_url);
 document.getelementbyid ("text"). innerhtml="Get the full path of the file domain:" + file_url;
}
</script>
<H1>js method to get the full path of the file domain,Compatible with different browsers</h1>
<div></div>
<input type="file" onchange="getvl (this)" />

The above code is used normally in ie 6 7 8Under ie9, document.selection.createrange () denied access,It seems that security has improved.

The final test found thatUnder ie9, if the file control gets focus,Document.selection.createrange () denies access,

Therefore, just add 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");
  • Previous Two JavaScript AES encryption methods (can be encrypted and decrypted with Java)
  • Next Digital puzzle game based on Vuejs