0

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>

Sainath
  • 979
  • 2
  • 13
  • 22

2 Answers2

1

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.

Dacker
  • 892
  • 2
  • 8
  • 12
  • thats right @dacker but even after adding it it does not work i uploaded a file less than 1 mb and it worked but for a file with 4mb it gives this message – Sainath Jun 13 '15 at 06:34
  • 4MB sounds exactly like that limit. The requestLimit default was like 28 MB. Not sure whether you set it correctly. Can you also add the IIS7 setting? Sometimes you need both settings. So add this in system.Webserver – Dacker Jun 13 '15 at 07:09
  • i have added maxallowedcontentlength in my web config like it said but still does not work.1073741824 is in bytes and when i convert it to its almost 1 gb but file upload does not accept more than 4 mb like in this tutorial [other stack overflow link](http://stackoverflow.com/questions/3853767/maximum-request-length-exceeded?rq=1) – Sainath Jun 13 '15 at 07:50
  • Can you post your Web.config? And I assume it's the application's root Web.config and you don't have additional Web.config on subdirectories. – Dacker Jun 13 '15 at 09:55
  • have a look at the web.config i added into the question – Sainath Jun 14 '15 at 12:15
  • 1
    Add maxRequestLength="1048576" executionTimeout="100000" to httpRuntime tag. Thought you had that and we were trying out both settings together. – Dacker Jun 14 '15 at 20:54
  • the issue was fixed i have updated the web.config in my question – Sainath Jun 16 '15 at 16:56
  • @sainath What was the fix exactly? – Dacker Jun 16 '15 at 21:16
  • It was the web config. I added a httpruntime tag but there was already one so it would thrown me an error but when i added it to the existing one it worked @dacker – Sainath Jun 20 '15 at 12:58
  • @sainath if the setting in my answer helped, please mark it as answer so other people know it's worth a try when they have the same issue – Dacker Jun 21 '15 at 13:02
0

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.)

Reza Del
  • 763
  • 10
  • 30