tags:

views:

220

answers:

2

I've created code to retrieve an image from the file system using an ASHX handler. The code displays the image correctly in Chrome, but I get a broken image in IE:

public class ImageHandler : IHttpHandler
{

    private int? QS_ImageID
    {
        get
        {
            int? temp = null;

            if (HttpContext.Current.Request.QueryString["ImageID"] != null)
                temp = HA.Utility.DataHelper.ParseDbInt(HttpContext.Current.Request.QueryString["ImageID"]);

            return temp;
        }

    }

    private bool QS_Thumbnail
    {
        get
        {
            bool thumbNail = false;
            if (HttpContext.Current.Request.QueryString["Thumbnail"] != null)
            {
                if (HttpContext.Current.Request.QueryString["Thumbnail"].Equals("1"))
                    thumbNail = true;
            }

            return thumbNail;
        }

    }

    public void ProcessRequest(HttpContext context)
    {
        if (QS_ImageID.HasValue)
        {
            int uploadID = QS_ImageID.Value;
            context.Response.ClearHeaders();
            context.Response.ClearContent();
            context.Response.Cache.SetNoStore();  
            context.Response.ContentType = UploadDAL.GetMetaData(uploadID).UploadContentType;
            context.Response.AddHeader("Content-Disposition", "inline;");

            if (QS_Thumbnail)
            {

                byte[] myImage = UploadDAL.GetFileThumbNail(uploadID);
                context.Response.AddHeader("Content-Length", myImage.GetLength(0).ToString());
                context.Response.BinaryWrite(myImage);
                context.Response.End();
            }
            else
            {
                byte[] myImage = UploadDAL.GetFile(uploadID);
                context.Response.AddHeader("Content-Length", myImage.GetLength(0).ToString());
                context.Response.BinaryWrite(myImage);
                context.Response.End();
            }
        }
    }

    public bool IsReusable
    {
        get
        {
            return false;
        }
    }

}
+1  A: 

The problem could be that you didn't specify a mime type for the file. Here are some common image mime types, from a function meant to return the corresponding mime type of a file:

string getContentType(String path)
{
    switch (Path.GetExtension(path))
    {
        case ".bmp": return "Image/bmp";
        case ".gif": return "Image/gif";
        case ".jpg": return "Image/jpeg";
        case ".png": return "Image/png";
        default : break;
    }
    return "";
}

If the image is physically stored on the hard drive, you can use this piece of code as it is, if not at least it can give you an idea about how to determine the mime type. In the ProcessRequest method, you would then use

context.Response.ContentType = getContentType(imageFileName);

Of course as i said before, if you don't have a physical path for the file (for example if it's stored in the database) you should still know the image type.

Hope this helps,
Andrei

scripni
A: 

IE does require a Mime type.

The following I use for getting the mime types of images, or force it to be a regular file and let the system handle it.

objMIMEType = Microsoft.Win32.Registry.GetValue("HKEY_CLASSES_ROOT\\." + objAttachment.Extension, "Content Type", "application/octetstream");

        if (objMIMEType != null)
            strMIMEType = objMIMEType.ToString();
        else
        {
            strMIMEType = "application/octetstream";
            context.Response.AddHeader("Content-Disposition", "attachment; filename=" + objAttachment.FullFileName);
            context.Response.AddHeader("Content-Length", objAttachment.AttachmentData.Length.ToString());
        }
Ryan Ternier