File size validation [Resolved]

Posted by Shahanaakter under ASP.NET on 4/20/2016 | Points: 10 | Views : 1528 | Status : [Member] | Replies : 2
Hi,Here is File size validation code. But it doesn't checking validation.
<script type="text/javascript">
function UploadFile(fileUpload) {
if (fileUpload.value != '') {
var uploadControl=document.getElementById("<%=btnUpload.ClientID %>").click();
} if (uploadControl.files[0].size > 1024) {
document.getElementById('lblMsg').style.display = "block";
return false;
}
else {
document.getElementById('lblMsg').style.display = "none";
return true;
}
}
</script>
</head>
<body>
<form id="form1" runat="server">

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

<asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" ControlToValidate="FileUpload1" ErrorMessage="RegularExpressionValidator" ValidationExpression="\.pdf|\.doc"></asp:RegularExpressionValidator><br />
<asp:Label ID="lblMsg" runat="server" Text="File uploaded successfully." ForeColor="Green"Visible="false" />

<asp:Button ID="btnUpload" Text="Upload" runat="server" OnClick="Upload" Style="display: none" />

</form>
</body>
</html>

Feel free to share informations


Responses

Posted by: A2H on: 5/5/2016 [Member] [MVP] Silver | Points: 50

Up
0
Down

Resolved
You also need to hide the label using css display property otherwise you will null reference exception at runtime.

  <asp:Label ID="lblMsg" runat="server" Text="File uploaded successfully." ForeColor="Green" Style="display: none" />


Thanks,
A2H
My Blog

Shahanaakter, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: A2H on: 5/5/2016 [Member] [MVP] Silver | Points: 25

Up
0
Down
You are not attaching the Javascript function to upload control thats the reason function is not getting called. You can use onchange event of upload control like below
 <asp:FileUpload ID="FileUpload1" runat="server" onchange="UploadFile(this)" />


Updated Javascript Code
 <script type="text/javascript">
function UploadFile(fileUpload) {
if (fileUpload.value != '') {
var uploadControl = document.getElementById("<%=btnUpload.ClientID %>").click();
} if (fileUpload.files[0].size > 1024) {
document.getElementById('lblMsg').style.display = "block";
return false;
}
else {
document.getElementById('lblMsg').style.display = "none";
return true;
}
}
</script>


Thanks,
A2H
My Blog

Shahanaakter, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response