Click here to Skip to main content
15,910,877 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have web form where I need to upload a file. I have set the maximum file size size as 10mb in web.config file. Now I need to validate the file upload in the form. If I try to upload a file size greater then 10mb it is moving to HTTP Error 404.13 - Not Found page but instead I need to display a error message in the same form as The maximum file has been extended . I used the following code for that purpose.

HTML
protected void btnSave_OnClick(object sender, EventArgs e)
   {
       if (uxFileUpload.FileBytes.Length > 0 )
       {
           int iMaxFileSize = 2097152;
           if (Request.ContentLength > iMaxFileSize)
           {
               exceedErrormessage.Visible=true;

               //Server.Transfer("Error.aspx");

           }


ASP.NET
<div class="file-upload">
        <asp:Label ID="uiFileUpload" runat="server" AssociatedControlID="uxFileUpload" Text="Select file" />
            <p>The maximun file size is 20mb</p>
        <asp:FileUpload ID="uxFileUpload" runat="server" />
            <asp:PlaceHolder ID="exceedErrormessage" Visible="false" runat="server">
                 <asp:Literal ID="exServerErroemessage" runat="server" Text="The maximum file limit has been exceeded" />
    </asp:PlaceHolder>
            <%--<asp:Label ID="label" runat="server"></asp:Label>--%>
            <asp:CustomValidator ID="CustomValidator1" runat="server" ControlToValidate="uxFileUpload"
   ErrorMessage="File size should not be greater than 20 mb." OnServerValidate="btnSave_OnClick"></asp:CustomValidator>
        <asp:Button ID="btnSaveDocument" ValidationGroup="SaveFile" OnClick="btnSave_OnClick" runat="server" Text="Upload" CssClass="button" />
    </div>


But still it is moving to the same error page when I try to upload a file size greater then 10mb. I even tried placing a breakpoint and checked where the problem is occurring. I found that when I try to upload a file size greater than 10 mb it is not even moving to the btnSave_OnClick function but otherwise if I upload a file size less than 10 mb it is moving to the function and the corresponding operation is been performed. Can anyone help me to solve this problem.
Posted
Comments
Sinisa Hajnal 30-Mar-15 6:40am    
Remove the limit from your config...or set up custom error page (your Error.aspx) that you will map to 404.13

1 solution

Hi the value you have taken 2097152 is in bytes which is equals to 2mb only but not 20mb. so please change this first to appropriate value which is 20971520
 
Share this answer
 
v2
Comments
user 3008 30-Mar-15 9:13am    
I now changed the value but still getting the same error message page. Actually the control is going to the function when the file size is been exceeded. That is the issue that if I provide conditions still it is not working. Can you provide me with any other alternatives

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900