1

In my page when I upload a file of size more than 4 MB it shows that the connection was reset. The reason is because the file upload limit is less than 4 MB. There is a solution to this on link

But, instead I want to show the error on the same page and not redirect. How can I do this?

Community
  • 1
  • 1
user1509
  • 1,131
  • 5
  • 17
  • 44

1 Answers1

2

You should read the file size and display errors (or not) before uploading (Saving the file)

protected void UploadButton_Click(object sender, EventArgs e)
{

    if(FileUpload1.HasFile)
    {
        if(FileUpload1.PostedFile.ContentLength > 4096)
        {
            ErrorLabel.Text = "The file you are trying to upload exceeds the allowed limit.";
        }
        else
        {
            string SavePath = "TheLocationTheFilesSaves";
            FileUpload1.SaveAs(SavePath + FileUpload1.FileName);
            // Do stuff
        }
    }

}
TheGeekYouNeed
  • 7,479
  • 2
  • 25
  • 42