2

ASHX ハンドラーを使用してファイル システムからイメージを取得するコードを作成しました。コードは Chrome では画像を正しく表示しますが、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;
        }
    }

}
4

2 に答える 2

3

問題は、ファイルの MIME タイプを指定していない可能性があります。ファイルの対応する MIME タイプを返すことを意図した関数から、いくつかの一般的な画像 MIME タイプを次に示します。

string getContentType(文字列パス)
{
    スイッチ (Path.GetExtension(パス))
    {
        case ".bmp": return "Image/bmp";
        case ".gif": return "Image/gif";
        case ".jpg": return "Image/jpeg";
        case ".png": return "Image/png";
        デフォルト : ブレーク;
    }
    戻る "";
}

画像がハード ドライブに物理的に保存されている場合は、このコードをそのまま使用できますが、少なくとも MIME タイプを判別する方法についてのアイデアが得られるとは限りません。ProcessRequestメソッドでは、次に使用します

context.Response.ContentType = getContentType(imageFileName);

もちろん、前に言ったように、ファイルの物理パスがない場合 (たとえば、データベースに保存されている場合) は、画像の種類を知っておく必要があります。

これが役に立てば幸いです、
アンドレイ

于 2010-07-25T05:22:30.090 に答える
1

IE には Mime タイプが必要です。

以下は、画像の MIME タイプを取得するために使用するか、強制的に通常のファイルにしてシステムに処理させます。

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());
        }
于 2010-08-18T18:13:34.867 に答える