JS get input file Absolute path method (recommended) _javascript tips

Source: Internet
Author: User

Recently, because of the need to upload files to an Oracle blob, to get a file path of some problems, due to security reasons, the new browser does not support the direct access to the local URL, online found some methods, as follows:

<script type= "Text/javascript" >//fx Get File path method function Readfilefirefox (filebrowser) {try {netscape.security.
  Privilegemanager.enableprivilege ("Universalxpconnect"); catch (E) {alert (' cannot access local files due to browser security settings. To overcome this, follow these steps: (1) in the Address bar, enter "About:config";(2) Right-click and select new->boolean;
    (3) Enter the name "Signed.applets.codebase_principal_support" (without quotes) as a new preference; (4) Click OK and try reloading the file ');
  Return } var Filename=filebrowser.value; This step will get the full client path.
  The following is too complex to judge, and the following get IE is also very complex.
  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 is loaded:relative paths are not allowed.
    Please provide a absolute path to this file. ");
  Return } if (file.exists () = = False) {alert ("file" + FIlename + "' not found.");
  Return
return file.path; 
 (////////////////////= browser var Sys = {} based on different browsers; GETVL (obj) 
 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 an 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 to the file domain is:" +file_url; } </script>  

The above code in IE 6 7 8 are normal use, under IE9, Document.selection.createRange () denied access, it appears that security has improved.

The final test found that, under IE9, if the file control gets the focus, Document.selection.createRange () denies access,

So you just need to add a 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 JS get input File Absolute path method (recommended) is a small series to share all the content, hope to give you a reference, but also hope that we support the cloud-dwelling community.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.