I have a web page that is used to upload a file. There are two input fields on the form. One is of type text used to give the file a name in our database. The other is of type file to select the file to upload.
<input type="text" name="docName" id="docName" size="35" value="" />
<input type="file" name="importFile" id="importFile" size="35" onchange="refreshFileName();"/>
I want to use JavaScript to extract the actual file name (minus the file path) from the "importFile" field once it's selected and auto-populate the field "docName" with that value. My JavaScript looks like this:
function refreshFileName(){
var fileName = document.getElementById('importFile').value;
var str = fileName.split("\\");
var length = str.length;
document.getElementById('docName').value = str[length-1];
}
This seems to work just file in Chrome, Firefox and IE 10 or 11. But it doesn't seem to work in IE 9 or earlier versions. Nothing I have tried seems to solve the problems. Any suggestions on how I can get this to work with earlier version of IE (I've already thought of the idea "just tell my users not to use IE").