<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 ();">