I am using below code to create a file upload control:
ASP code :
<label for="imageFileUpload" class="file-button new-gradient " tabindex="0">
<span>
<%=(string.Format("{0}", (UploadedFiles != null && UploadedFiles.Count > 0) ? "Add Documentation" : "Add Receipt" %>
</span>
<asp:FileUpload ID="imageFileUpload" runat="server" type="file" size="10" CssClass="file-upload visually-hidden"`enter code here`
onchange="PerformUpload();" ClientIDMode="Static" />
</label>
the class new-gradient
is used to add look and feel to default File browser button and label hides the default file browser button..
Javascript code:
$(function() {
$("#imageFileUpload").on("keyup", function (event) {
if (event.keyCode === 13) {
$("input[type = 'file']").click();
}
});
});
I am able to set the focus on the button using tabIndex. When i use keyboard to access the button focus is present but the file browser is not opened. Nothing happens when i hit enter from keyboard. When clicked with mouse the file browser opens up and everything works properly.
I have followed what is suggested in this Link
Is it possible to use the keyboard to trigger a File Browser in JavaScript? but it was not able to solve my question.
Could anyone help me out. Thanks in advance.