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

    Restricting image size in file upload control using apt.net 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 asp.net 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.
    http://programming.top54u.com/post/ASP-Net-FileUpload-Size-Limit-Example.aspx

    Thanks
    Shekar.Y

  • #535865
    refer this link
    http://forums.asp.net/t/1086821.aspx

    _______________________________
    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
    Hi

    Please check the following code:

    Design
    ------

    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
    <title>Untitled Page</title>
    </head>
    <body>
    <form id="form1" runat="server">
    <div>
    <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" />
    </div>
    </form>
    </body>
    </html>

    C# Code Behind
    --------------

    protected void Button1_Click(object sender, EventArgs e)
    {
    Response.Write(FileUpload1.PostedFile.ContentLength);
    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;
    }
    else
    {
    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
    chirag.madhani@yahoo.com

  • #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.