1

I made a webusercontrol to upload images and document, so when the user uploads an image file, the file goes to Images folder and when user uploads a doc file it goes to Documents folder. It seems I have a problem when I'm trying to upload file with long name or file that has big file size, error page always appear and System.Web.HttpException: Maximum request length exceeded written on that page. How can I solve this problem with a try-catch-block?

protected void btnsubmit_Click(object sender, EventArgs e)
{

    string [] img = {"bmp","gif","png","jpg","jpeg"};
    string ext = System.IO.Path.GetExtension(FileUpload.PostedFile.FileName);  
    bool isValidFile = false;
    for (int i = 0; i < img.Length; i++)
    {
    if (ext == "." + img[i] )
    {
        isValidFile = true;
        break;
    }
    }
    if (!FileUpload.HasFile)
    {
        lblMessage.Text = "You must select a file before pressing the submit button!";
    }
    else if (StoreFolders.ToString() == "Images/")
    {
        if (!isValidFile)
        {
            lblMessage.Text = "You can only upload Image files!";
        }
        else
        {
                FileUpload.PostedFile.SaveAs(Server.MapPath(StoreFolders) + FileUpload.FileName);
                lblMessage.Text = "Your file is saved succesfully!";
                Dtable.Rows.Add(FileUpload.FileName, "(" + GetSize(FileUpload.FileBytes.Length) + " )", DateTime.Now);
        }

    }
    else if(StoreFolders.ToString() == "Documents/")
    {
        if (isValidFile)
        {
            lblMessage.Text = "You can only upload Document files!";
        }
        else
        {
            FileUpload.PostedFile.SaveAs(Server.MapPath(StoreFolders) + FileUpload.FileName);
            lblMessage.Text = "Your file is saved succesfully!";
            Dtable.Rows.Add(FileUpload.FileName, "(" + GetSize(FileUpload.FileBytes.Length) + " )", DateTime.Now);
        }

    }
    GridView1.DataSource = Dtable;
    GridView1.DataBind();

}
JSON C11
  • 11,272
  • 7
  • 78
  • 65
AHA27
  • 31
  • 1
  • 3
  • 12

1 Answers1

1

The first thing you'll want to do is configure your app to allow the expected file size, as described here:

Maximum request length exceeded

Then you can try something like the answer here: Catching "Maximum request length exceeded"

Community
  • 1
  • 1
Colin
  • 4,025
  • 21
  • 40