0

My error is Maximum request length exceeded. I want file upload must be smaller than 2MB. Please help me to fix code bellow, thanks

my controller:

public ActionResult Index()
        {
            var path = "~/Images/upload/";

            if (Request.Files["UpFile"] != null && Request.Files["UpFile"].ContentLength < 2048)
            {
                var upload = Request.Files["UpFile"];
                upload.SaveAs(Server.MapPath(path + upload.FileName));
            }
            else
            {
                ModelState.AddModelError("", "The size of file too big");
            }

            return View();
        }
giathienphu
  • 137
  • 3
  • 14

1 Answers1

1

Try to manage your maximum request length for reducing errors to minimum: Maximum request length exceeded

I think it's a good practice to use try..catch when working with uploading files even if you have global exception handler.

Community
  • 1
  • 1
Sergey Shabanov
  • 176
  • 2
  • 11