I have a Fileupload control and an img tag in my aspx page where I can upload image and show in img tag.
<asp:FileUpload ID="f_UploadImage" runat="server" />
<br />
<img id="myUploadedImg" alt="Photo" style="width: 180px;" />
I am not using HTML input tag for some reasons. By using below code I am accessing fileupload change trigger which is work fine while clicking on Choose File option.
<script type="text/javascript">
$("#f_UploadImage").on('change', 'input', function () {
var file, img;
if ((file = this.files[0])) {
img = new Image();
img.onload = function () {
sendFile(file);
};
img.onerror = function () {
alert("Not a valid file:" + file.type);
};
img.src = _URL.createObjectURL(file);
}
});
</script>
Problem is: When I put another HTML button id (btn_upload) in my aspx page and when click on that button I enable access to fileupload click event but when I choose file my File Upload on change trigger not fire. Below is the code which I was trying with another button.
$("#btn_upload").on('click', function () {
$('#f_UploadImage').trigger('click');
});
$("#f_UploadImage").on('change', 'input', function () {
var file, img;
if ((file = this.files[0])) {
img = new Image();
img.onload = function () {
sendFile(file);
};
img.onerror = function () {
alert("Not a valid file:" + file.type);
};
img.src = _URL.createObjectURL(file);
}
});