I have the following HTML / JS code:
<div class="file_upload">
<input type="file" id="file_upload" name="file_upload" onchange="g();">
</div>
<script>
function g() {
var fileName = document.getElementById("file_upload").value;
fileName = fileName.substring(fileName.lastIndexOf('/')+1);
alert(fileName);
}
</script>
I want that after user upload a file, than the file name will displlay in alert the problem in this cide is that the path & the file name are diplayed and I want the file name only.
How can I fix it?
/
to split off the filename, it won't match `\`. Quick solution would be: fileName = fileName.replace('\\', '/').substring(fileName.lastIndexOf('/') + 1); – JimmiTh Jul 13 '13 at 9:59