How does js obtain the complete path of the file control?

Source: Internet
Author: User

<Script type = "text/javascript">
// Method for obtaining the file path using FX
Function readFileFirefox (fileBrowser ){
Try {
Netscape. security. PrivilegeManager. enablePrivilege ("UniversalXPConnect ");
}
Catch (e ){
Alert ('cannot access local files because of browser security settings. To overcome this problem, follow these steps: (1) in the address bar, enter "about: config"; (2) Right-click and choose New-> Boolean; (3) enter "signed. applets. codebase_principal_support "(without quotation marks) as the name of a new preference; (4) Click OK and try to reload the file ');
Return;
}
Var fileName = fileBrowser. value; // you can obtain the complete path of the client. Whether or not the following statements are too complex, and the ie statements below are also very complicated.
Var file = Components. classes ["@ mozilla.org/file/local1_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 + "'Could not 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;
}
// Obtain the path from different browsers
Function getvl (){
// 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. Internet <= "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 obtained path is an encrypted string recognized by FF.
File_url = readFileFirefox (document. getElementById ("file "));
}
// Alert (file_url );
Document. getElementById ("text"). innerHTML = "obtain the complete path of the file domain:" + file_url;
}
</Script>
<H1> javascript can obtain the complete path of a file domain, which is compatible with different browsers <Div id = "text" style = "color: # f00;"> </div>
<Input type = "file" id = "file"/>
<Input name = "" type = "button" value = "get" 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.