JS how to get the full path of the file control

Source: Internet
Author: User

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


}


//Access paths based on different browsers


function Getvl () {


//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 = document.getElementById ("file"). Value;


}else if (sys.ie>= "7.0") {


//IE7,IE8


var file = document.getElementById ("file");


File.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 (document.getElementById ("file"));


}


//alert (File_url);


document.getElementById ("text"). Innerhtml= "Get the complete path to the file domain is:" +file_url;


}


</script>


<h1>js Gets the full path to the file domain, compatible with different browsers </h1>


<div id= "text" style= "color: #f00;" ></div>


<input type= "file" id= "file"/>


<input name= "type=" button "value=" gets "onclick=" GETVL (); >

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.