I have a hidden file input with a fake button and input for browser display consistency. I currently have the original input visible as well, and have discovered that using it to upload the file everything runs fine.
However using the button in "dummyfile" to trigger the click via javascript the value is loaded as expected, as well as in the UI. But when I click submit this time, it instead clears the value from the input and does nothing else. This only happens in IE, not in chrome. It is the source of the problem in a question I asked previously: https://stackoverflow.com/questions/19275901/internet-explorer-specific-form-post-with-file-input-returning-empty-file-list. I ask this as a separate question because it is relevant beyond the scope of that one.
HTML (this is in a partial view so I can add more inputs and return a file collection, I just pulled it out to the main view and hard coded the 0 in for this and another bug).
<div class="control-group">
<label class="control-label" for='@String.Format("file{0}", 0)'>File</label>
<div class="controls">
<input type="file" name="files" class="hiddenFileInput" id='@String.Format("file{0}", 0)' />
<div class="dummyfile">
<input id="@String.Format("filename{0}", 0)" type="text" class="input disabled" name="filename" >
<button type="button" class="btn btn-primary">Choose</button>
</div>
</div>
</div>
jQuery:
function fileUploadControlInit() {
$('.dummyfile .btn').unbind("click").on("click", function (e) {
$(this).closest("div").siblings(" :file").trigger('click');
});
$('.hiddenFileInput').on("change", function (e) {
var val = $(this).val();
var file = val.split(/[\\/]/);
var fileName = file[file.length - 1];
$(this).siblings("div").children(" :text").val(fileName);
$("#UploadFile").prop('disabled', false);
});
}