Forum Discussion
Can't able to Upload Images on Azure Server if it is more than 128KB
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 .
1 Reply
- UdhayarajanJCopper Contributor
Base64 bloats your payload. Instead of encoding to base64, upload the image as a binary stream (which is the standard for image uploads).
Change your code to:
Skip base64 encoding.
Store the image as a byte array directly from the stream.
Note: If your app must use Base64 (e.g., to store in DB), ensure that the client uploads the image using multipart/form-data with the correct Content-Type: image/jpeg headers.