简体   繁体   中英

“Maximum request length exceeded” error even after setting maxAllowedContentLength

I'm using IIS 6.2 and my solution has a file-upload control and trying to upload a bunch of images but i get this error.I have searched the internet and got a lot of solutions but none of them worked.

I have applied maxAllowedContentLength="1073741824" but still get the same error.

protected void lnkbtnUpload_Click(object sender, EventArgs e)
{
    try
    {
        foreach (HttpPostedFile objHttpPostedFile in fuUpload.PostedFiles)
        {
            string FileName = objHttpPostedFile.FileName;
            string FileType = objHttpPostedFile.ContentType;
            Stream fs = objHttpPostedFile.InputStream;
            BinaryReader br = new BinaryReader(fs);
            Byte[] bytes = br.ReadBytes((Int32)fs.Length);

            using (SqlConnection con = new SqlConnection(ConnectionString))
            {
                con.Open();
                SqlCommand cmd = new SqlCommand("uspInsertImage", con);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add("Filename", SqlDbType.NVarChar).Value = FileName;
                cmd.Parameters.Add("FileType", SqlDbType.NVarChar).Value = FileType;
                cmd.Parameters.Add("ImageStream", SqlDbType.VarBinary).Value = bytes;
                cmd.Parameters.Add("DateCreated", SqlDbType.DateTime).Value = DateTime.Now;
                int i = cmd.ExecuteNonQuery();
                con.Close();
            }

            BindImage();
        }
    }
    catch(Exception ex)
    {
        Console.WriteLine(ex.Message);
    }
}

My Web.Config File

<system.web>
  <compilation debug="true" targetFramework="4.5" />
  <httpRuntime targetFramework="4.5" />
</system.web>    
<system.webServer>
  <security>
    <requestFiltering>
      <requestLimits maxAllowedContentLength="4073741824" />
    </requestFiltering>
  </security>

requestLimits settings have been added since IIS 7.0.

For IIS 6 you need to use:

<system.web>
    <httpRuntime maxRequestLength="1048576" executionTimeout="100000" />
</system.web>

This allows a file upload of 1 GB and it will time out after 100,000 seconds, or 27.8 hours.

Maximum request limited is there to protect your website against denial of service attacks so it's best to expand the file-size limit for specific directories rather than your entire application you can do it with

<location path="Upload">
<system.web>
    <httpRuntime executionTimeout="110" maxRequestLength="1048576" />
</system.web>

and you can use the code below to show a warning to the users when they try to upload something higher than max limit, adding a warning will improve your website UX

    System.Configuration.Configuration config = WebConfigurationManager.OpenWebConfiguration("~");
HttpRuntimeSection section = config.GetSection("system.web/httpRuntime") as HttpRuntimeSection;
double maxFileSize = Math.Round(section.MaxRequestLength / 1024.0, 1);
FileSizeLimit.Text = string.Format("Make sure your file is under {0:0.#} MB.", maxFileSize);

Note: maxRequestLength is measured in kilobytes, which is why the values differ in this config example. (equivalent to 1 GB.)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM