Hi nauna,
Instead of Buttob click event use input type file change event.
Refer below code.
HTML
<input type="file" name="postedFile" />
<progress id="fileProgress" style="display: none"></progress>
<hr />
<span id="lblMessage" style="color: Green"></span>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script type="text/javascript">
$(function () {
$("input[type='file']").on("change", function () {
$.ajax({
url: 'Handler.ashx',
type: 'POST',
data: new FormData($('form')[0]),
cache: false,
contentType: false,
processData: false,
success: function (file) {
$("#fileProgress").hide();
$("#lblMessage").html("<b>" + file.name + "</b> has been uploaded.");
},
xhr: function () {
var fileXhr = $.ajaxSettings.xhr();
if (fileXhr.upload) {
$("progress").show();
fileXhr.upload.addEventListener("progress", function (e) {
if (e.lengthComputable) {
$("#fileProgress").attr({
value: e.loaded,
max: e.total
});
}
}, false);
}
return fileXhr;
}
});
});
});
</script>
Rest Handler code will remain as it is.