get file path from browse button - do not upload the file
I have multiple browse buttons on my page. I want to save the path of the files selected by the user and not upload the files themselves. Security i开发者_运维问答s not an issue since the page is on on intranet.
Is it possible to get the file path (file name included) so that I can store this value in the database?
Nope, you can not get it reliably.
For example, some browsers will return c:\fakepath\file.jpg
.
Assuming your has an id of upload this should hopefully do the trick:
var fullPath = document.getElementById('upload').value;
if (fullPath) {
var startIndex = (fullPath.indexOf('\\') >= 0 ? fullPath.lastIndexOf('\\') : fullPath.lastIndexOf('/'));
var filename = fullPath.substring(startIndex);
if (filename.indexOf('\\') === 0 || filename.indexOf('/') === 0) {
filename = filename.substring(1);
}
alert(filename);
}
From How to get the file name from a full path using JavaScript?
精彩评论