Can't able to Upload Images on Azure Server if it is more than 128KB

Muhammed Sahid M A 0 Reputation points
2025-06-13T07:21:07.5033333+00:00

Hi Everyone ,

I am trying to upload image and store  but when i do it in my local server its working fine but when i push the same code in server i am getting entity  too large 

I reached out microsoft support they are telling that "The content type format is supposed to be image as example given to upload the image file otherwise the application gateway will not understand the image and tries to accept only 128KB which is why the request is not accepted with Application Gateway.

"

when i am debugging i am getting same content type = image/jpeg . Code snippet for storing 

private void SaveImage(int vendItemID) {

try {

  string folderPath = @ "C:\UploadedImages\";          Directory.CreateDirectory(folderPath);

  string fileExtension = Path.GetExtension(fileUpload.FileName).ToLower();

  string fileName = Path.GetFileNameWithoutExtension(fileUpload.FileName);

  string newImageName = string.Empty;

  dbCon.Server = "***";

  dbCon.OpenConnection();

  // Optimize and compress the image          byte[] optimizedImageBytes = CompressImageToTargetSize(fileUpload.PostedFile.InputStream, fileExtension);

  // Convert to Base64          string base64String = Convert.ToBase64String(optimizedImageBytes);

  using(SqlCommand cmd = new SqlCommand("***", dbCon.Cnn)) {

     cmd.CommandType = CommandType.StoredProcedure;

     AddImageParameters(cmd, vendItemID, fileName, fileExtension);

     cmd.Parameters.AddWithValue("@ImageBase64", base64String);

     var outputCodeParam = new SqlParameter("@ImageNewName", SqlDbType.NVarChar, 200) {

        Direction = ParameterDirection.Output

     };

     cmd.Parameters.Add(outputCodeParam);

     cmd.ExecuteNonQuery();

     newImageName = outputCodeParam.Value.ToString();

  }

  string newFilePath = Path.Combine(folderPath, newImageName);

  File.WriteAllBytes(newFilePath, optimizedImageBytes);

  UpdateImagePath(newImageName, newFilePath);

} catch (Exception ex) {

  ShowMessage("Error while saving image: " + ex.Message, Color.Red);

  LogError("Image save failed", ex);

  throw;

} finally {

  dbCon.CloseConnection();

}

}

I also try to compress and change it to base 64 but its not working. Kindly assist on what steps i need to take. Thanks, Shahid

Note :WebForm i m working on .

C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
11,553 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Muhammed Sahid M A 0 Reputation points
    2025-06-19T04:45:25.4466667+00:00

    Hello everyone,

    I’d like to share a recent issue we encountered and how it was resolved, in case it helps others facing similar problems.
    While deploying our ASP.NET Web Forms application to the Microsoft Azure live server, we encountered the following error when attempting to upload large image files (approximately 2.5 MB):
    Request Entity Too Large

    Microsoft-Azure-Application-Gateway/v2

    Investigation:

    Initially, the application worked perfectly in our local environment and on-premises server after setting the following in web.config:
    <system.webServer>

    <security>

    <requestFiltering>
    
      <requestLimits maxAllowedContentLength="1552428800" />
    
    </requestFiltering>
    

    </security>

    </system.webServer>

    However, in Azure, the issue persisted. We realized the problem was due to the default request size limit on Azure Application Gateway, which is 128 KB for request bodies.

    🛠 Resolution:

    After multiple discussions and escalations, we confirmed that the Azure Application Gateway HTTP setting had to be updated.

    Steps:

    Go to your Azure Application Gateway resource.

    Under Settings, open HTTP Settings.

    Select the relevant HTTP setting.

    Enable "Override with new settings" if not already.

    Set "Request body size (KB)" to a higher value (we used 3000 KB).

    Save the changes and wait for the update to propagate.

    Once this was done, image uploads started working immediately without any additional changes on the code side.

    Thank You.

    1 person found this answer helpful.
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.