Click here to Skip to main content
15,888,579 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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)
    {
        //Get filename and extention
        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);

        //Check file extention
        if (fileExtention.ToLower() == "jpg" || fileExtention.ToLower() == "gif" || fileExtention.ToLower() == "bmp")
        {
            //Check Image file size
            if (fileSize <= 300)
            {
                //Get image stream
                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);

                //Upload document
                SelfServicePortal.UpdateSelfServiceEmployeePhoto(hfEmployeeNumber.Value, imgBinaryData);

                //Refresh Employee Photo and parent page
                SelectEmployeePhoto();
                Page.ClientScript.RegisterStartupScript(GetType(), "RefreshParentPage", "fncRefreshParentPage('" + hfRefreshClientID.Value + "');", true);
            }
            else
            {
                //Alert incorrect file size
                Page.ClientScript.RegisterStartupScript(GetType(), "AlertIncorrectFileSize", "fncAlertIncorrectFileSize();", true);
            }
        }
        else
        {
            //Alert incorrect extention
            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();
           }
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900