JS Method for Obtaining input file Absolute Path of Recommendation
- 2021-07-07 06:21:39
- OfStack
Recently, due to the need to upload files to oracle blob, I encountered some problems in obtaining the file path. Due to security reasons, the new version of the browser does not support directly obtaining the local URL, and 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 files due to browser security settings. To overcome this 1 Point, please follow these steps: (1) Enter in the address bar "about:config";(2) Right-click and select New->Boolean; (3) Input "signed.applets.codebase_principal_support" (without quotation marks) as the 1 The name of the new preference ;(4) Click OK And try to reload the file ');
return;
}
var fileName=fileBrowser.value; // This 1 Step to get the full path of the client. Whether the following is too complicated to judge, and the following is obtained ie It's 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){
// Judgment 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 path obtained is FF Recognized encrypted string
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 as: "+file_url;
}
</script>
<h1>JS The method of obtaining the complete path of the file domain is compatible with different browsers </h1>
<div id="text" style="color:#f00;"></div>
<input type="file" id="file" onchange="getvl(this)" />
The above code is used normally in IE 6 7 8. Under IE9, document. selection. createRange () denies access, which seems to improve security.
The final test found that under IE9, document. selection. createRange () denied access if the file control gained focus,
Therefore, just add a sentence 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");