You must Sign In to post a response.
  • Category: ASP.NET

    Restricting image size in file upload control using 2.0

    Dear Team,

    I have an file upload control in my applcation for uploading some logos.
    I want restrict that logo size to 40kb.
    can you help me how to restrict that image size using 2.0 c#.

    Thanks & Regards
  • #535862
    You can handle the file size from server side.i.e you can validate the upload file size on the server side using the ContentLength of the file.

    FileUploadConrol1.PostedFile.ContentLength will give you the length.

    Check the following link for example.


  • #535865
    refer this link

    Nisar Ahmad
    DotNetSpider MVM, Badge of Appreciation Award – 2010
    Greatness Can't be achieved by doing big things, If you really want to be great, Do small things in Great way.

  • #535870

    Please check the following code:


    <html xmlns="">
    <head runat="server">
    <title>Untitled Page</title>
    <form id="form1" runat="server">
    <asp:FileUpload ID="FileUpload1" runat="server" />
    <asp:CustomValidator ID="CustomValidator1" runat="server" ControlToValidate="FileUpload1"
    ErrorMessage="File Size Cannot Exceed 40 KB" OnServerValidate="CustomValidator1_ServerValidate"></asp:CustomValidator>
    <br />
    <br />
    <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Button" />

    C# Code Behind

    protected void Button1_Click(object sender, EventArgs e)
    if (Page.IsValid)
    FileUpload1.SaveAs(Server.MapPath("~/" + FileUpload1.FileName));

    protected void CustomValidator1_ServerValidate(object source, ServerValidateEventArgs args)
    if (FileUpload1.PostedFile.ContentLength >= 40960)
    args.IsValid = false;
    args.IsValid = true;

    Thanks & Regards
    Paritosh Mohapatra

    Thanks & Regards
    Paritosh Mohapatra
    Microsoft MVP (ASP.Net/IIS)
    DotNetSpider MVM

  • #535905
    You need to check it server side.. like

    if (FileUpload1.PostedFile.ContentLength >= 40000)
    //Prompt for size exceed...

    Thanks & Regards
    Anil Kumar Pandey
    Microsoft MVP, DNS MVM

  • #536076
    //below vl check the filesize

    FileUpload1.PostedFile.ContentLength >= 20000(or any lengh u want)

    it will upload file if it is greater than 20000 bytes

    Chirag - 48
    Enjoy With Errors

  • #536234
    Thank you so much friends for u r support.
    I think the code will helps me a lot.

This thread is locked for new responses. Please post your comments and questions as a separate thread.
If required, refer to the URL of this page in your new post.