0

How to validate the size and format of an image imported by the <Asp:FileUpload> tag?

I've tried using the code below, but it only checks if it's a valid image, but I do not know how to do those validations.

<div id="img2" style="width: 530px; height: 270px; border: 2px solid rgb(225, 226, 233); box-shadow: rgb(246, 246, 249) 0px 0px 5px inset; background-color: rgba(251, 235, 235, 0.4);">
  <asp:Image ID="imgBanner" runat="server" ImageUrl="~/" Style="border-width: 0px; position: inherit; min-height: 130px; min-width: 130px; top: 20px; bottom: 0; left: 0; right: 0; margin: auto; max-width: 130px; max-height: 130px;" />
</div>

<asp:FileUpload ID="fileUpload" runat="server" />

<script>    
  $("#<%=fileUpload.ClientID%>").on('change', function () {
    if (this.files[0].type.indexOf("image") > -1) {
      var reader = new FileReader();
      reader.onload = function (e) {
        $('#<%=imgBanner.ClientID%>').attr('src', e.target.result);
      }
      reader.readAsDataURL(this.files[0]);
    } else {
      $('#<%=imgBanner.ClientID%>').attr('src', '');
      criarDivAlert('Não é uma imagem válida')
    }
  });
</script>
Rory McCrossan
  • 331,213
  • 40
  • 305
  • 339

0 Answers0