Trigger click event of input file from external button

21 Dec 20222 minutes to read

You can trigger the click event of input file from external button using click event of button. In the below sample, triggered click event of input file from Essential JavaScript 2 Button.

<div class="col-lg-8 control-section">
    <div class="control_wrapper">
        <div id="dropArea">
            <span id="drop"> Drop image (JPG, PNG) files here or <button class='e-btn e-control' id="browse">Browse</button></span>
        </div>
        @Html.EJS().Uploader("UploadFiles").DropArea("#drop").AsyncSettings(new Syncfusion.EJ2.Inputs.UploaderAsyncSettings { SaveUrl = "https://services.syncfusion.com/aspnet/production/api/FileUploader/Save", RemoveUrl = "https://services.syncfusion.com/aspnet/production/api/FileUploader/Remove" }).Render()
    </div>
</div>

<script>

    window.onload = function () {
        document.getElementById('browse').onclick = () => {
            document.getElementsByClassName('e-file-select-wrap')[0].querySelector('button').click();
        };
    }

</script>
.control_wrapper {
        max-width: 500px;
        margin: auto;
    }
    #dropArea {
        border: 1px dashed #c3c3cc;
        text-align: center;
        padding: 20px 0 10px;
    }

    .e-file-select-wrap {
        display: none;
    }

    .control_wrapper .e-upload .e-upload-drag-hover {
        margin: 0;
    }

NOTE

You can also explore ASP.NET MVC File Upload feature tour page for its groundbreaking features. You can also explore our ASP.NET MVC File Upload example to understand how to browse the files which you want to upload to the server.