Get full path without filename in Javascript
how to use javascript to get full file path
const onChange = (event) => {
const value = event.target.value;
// this will return C:\fakepath\somefile.ext
console.log(value);
const files = event.target.files;
//this will return an ARRAY of File object
console.log(files);
}
return (
<input type="file" onChange={onChange} />
)
/* Found from Stack Overflow */
Example
<!DOCTYPE HTML>
<html>
<head>
<title>
How to get the file name from a
full path using JavaScript
</title>
</head>
<body style = "text-align:center;">
<h1 style = "color:green;" >
GeeksForGeeks
</h1>
<p id = "GFG_UP" style =
"font-size: 15px; font-weight: bold;">
</p>
<button onclick = "gfg_Run()">
get File Name
</button>
<p id = "GFG_DOWN" style =
"color:green; font-size: 20px; font-weight: bold;">
</p>
<script>
var el_up = document.getElementById("GFG_UP");
var el_down = document.getElementById("GFG_DOWN");
var path = "Path = " +
"C:\\Documents\\folder\\img\\GFG.jpg";
el_up.innerHTML = path;
function gfg_Run() {
el_down.innerHTML = path.replace(/^.*[\\\/]/, '');
}
</script>
</body>
</html>
Output
