I have a web application that is supposed to display a user's profile image and allow them to upload a new profile picture. The supported files are .jpg, .gif, .bmp. If I load the application in Chrome or Firefox and upload a .gif image, the images load properly but in Internet Explorer, it doesn't load at all. If I type in the file location of the .gif image directly in the browser in IE11, it displays the image.
I'm not sure if it's a browser or application issue.
This is the code that checks for the correct file extension and file size:
protected void btnUploadPhoto_Click(object sender, EventArgs e)
{
string fileName = flUploadPhoto.PostedFile.FileName.Remove(0, flUploadPhoto.PostedFile.FileName.LastIndexOf("\\") + 1);
string fileExtention = fileName.Remove(0, fileName.LastIndexOf(".") + 1);
int fileSize = (flUploadPhoto.PostedFile.ContentLength / 1024);
if (fileExtention.ToLower() == "jpg" || fileExtention.ToLower() == "gif" || fileExtention.ToLower() == "bmp")
{
if (fileSize <= 300)
{
Stream imgStream = flUploadPhoto.PostedFile.InputStream;
int imgLen = flUploadPhoto.PostedFile.ContentLength;
string imgContentType = flUploadPhoto.PostedFile.ContentType;
byte[] imgBinaryData = new byte[imgLen];
int n = imgStream.Read(imgBinaryData, 0, imgLen);
SelfServicePortal.UpdateSelfServiceEmployeePhoto(hfEmployeeNumber.Value, imgBinaryData);
SelectEmployeePhoto();
Page.ClientScript.RegisterStartupScript(GetType(), "RefreshParentPage", "fncRefreshParentPage('" + hfRefreshClientID.Value + "');", true);
}
else
{
Page.ClientScript.RegisterStartupScript(GetType(), "AlertIncorrectFileSize", "fncAlertIncorrectFileSize();", true);
}
}
else
{
Page.ClientScript.RegisterStartupScript(GetType(), "AlertIncorrectExtention", "fncAlertIncorrectExtention();", true);
}
}
Front-end for the image:
<div id="divImage" runat="server" class="thumbnail employee-profile-img" href="#" style="padding-right: 5px;">
<img runat="server" id="imgBC" class="navPortrait" />
</div>
What I have tried:
I've tried using MemoryStream and FileStream but neither provides a working solution:
var image = System.Drawing.Image.FromStream(flUploadPhoto.PostedFile.InputStream);
var imageConverter = new System.Drawing.ImageConverter();
var returnedVal = (byte[])imageConverter.ConvertTo(image, typeof(byte[]));
using(MemoryStream mStream = new MemoryStream())
{
image.Save(mStream, image.RawFormat);
return mStream.ToArray();
}