Limit file upload size in asp.net

I want to limit the download of file size to a certain limit. But the problem is that I want to provide a pop-up warning when the download size is exceeded. But instead, the following error is displayed on the webpage

HTTP Error 404.13 - Not Found

The request filtering module is configured to deny a request that exceeds the request content length. Here is my code

 protected void Button1_Click(object sender, EventArgs e)
{
    if (fuDocpath.HasFiles)
    {
        try
        {
            DateTime now = DateTime.Now;
            lbldateStamp.Text = now.ToString("mm_dd_yyyy_hh_mm_ss");
            //string foldername = lblsessionID.Text + "_" + lbldateStamp.Text;
            string folderpath = (Server.MapPath("~/Uploaded_Files/") + lblsessionID.Text + "_" + lbldateStamp.Text + "/");
            Directory.CreateDirectory(folderpath);
            if (fuDocpath.PostedFile.ContentLength < 20970000)
            {
                try
                {
                    foreach (HttpPostedFile files in fuDocpath.PostedFiles)
                    {
                        string filename = Path.GetFileName(files.FileName);
                        string folderpath1 = folderpath + "/";
                        fuDocpath.SaveAs(folderpath1 + filename);
                        lblName.Text = lblName.Text + "|" + filename;
                        lblerror.Text = string.Empty;
                    }
                }
                catch (Exception eex)
                {
                    ClientScript.RegisterStartupScript(this.GetType(), "myalert", "alert('" + eex.Message + "')", true);
                }

            }
        }
        catch (Exception ex)
        {
            lblerror.Text = "File couldn't be uploaded." + ex.Message;
            lblName.Text = string.Empty;
        }
    }
}
+4
source share
2 answers

Look at this - How to increase the maximum upload file size in ASP.NET?

You can change the maximum request size in web.config - 4Mb is used by default

+5
source

/, , - , . :

ASP.NET - , 4096 () ( 4 ) . , maxRequestLength Web.config.

. maxRequestLength Machine.config, (, ), maxRequestLength, . Microsoft Internet Explorer " DNS".

Machine.config, , @sh1rts.

0

Source: https://habr.com/ru/post/1547111/


All Articles