In an MVC application, I'm having users upload a file and then doing some error handling. This portion works fine.
The file input is:
<div class="col-md-3" id="browsebutton">
<label class="btn btn-primary btn-file">
Browse<input type="file" id="FileUpload" name="FileUpload" class="" />
</label></div>
I want to display the file name as soon as a user chooses it, so I added this bit of JavaScript and an onChange="getFileName():
function getFileName() {
str = document.getElementById("FileUpload").value;
var filename = str.substring(12, str.length);
document.getElementById("browsebutton").innerHTML += filename;
}
var coll = document.getElementsByClassName("collapsible");
var i;
This properly displays the filename. However, as soon as I add this, the FileUpload becomes null in my controller.
How is this JavaScript interfering with my file upload?
getFileNamehandler? I don't see how it could interfere with anything.innerHtml, which reassigns the entire contents back to the div, resulting in a cleared input element. You can instead create a new element and append that element tobrowsebuttonusing DOM methods, instead of appending a string.