使用Stream显示Image*.ico

use*_*850 5 c# asp.net stream

我想用扩展名显示图像,*.ico我用Stream它来做.但我有一个问题.

随着扩展*.jpg,*.bmp...图像显示确定,但*.ico它没有显示

这是我的代码:

 private void OutputStream(string fileName)
    {
        try
        {
            Stream dataStream = null;

                SPSecurity.RunWithElevatedPrivileges(delegate()
                {
                    SPFile spFile = web.GetFile(fileName);
                    dataStream = spFile.OpenBinaryStream();
                    this.HandleOutputStream(dataStream);
                });

        }
        catch (System.Threading.ThreadAbortException exTemp)
        {
            logger.Error("KBStreamPicture::OutputStream", exTemp);
        }
        catch (Exception ex)
        {
            //System.Diagnostics.Debug.Write("OutputStream::" + ex);
            logger.Error("KBStreamPicture::OutputStream", ex);
        }
    }
Run Code Online (Sandbox Code Playgroud)

private void HandleOutputStream(Stream dataStream)
    {
        const int bufferSize = 16384; //16KB 

        using (Stream file = dataStream)
        {
            if (file != null)
            {
                this.Page.Response.Clear();
                this.Page.Response.Cache.SetCacheability(System.Web.HttpCacheability.Private);
                this.Page.Response.Cache.SetExpires(DateTime.Now.AddMinutes(20)); //only cache 20 minutes
                byte[] buffer = new byte[bufferSize];
                int count = file.Read(buffer, 0, bufferSize);

                while (count > 0)
                {
                    if (this.Page.Response.IsClientConnected)
                    {
                        this.Page.Response.OutputStream.Write(buffer, 0, count);
                        count = file.Read(buffer, 0, bufferSize);
                    }
                    else
                    {
                        count = -1;
                    }
                }
                this.Page.Response.End();
            }
        }
    }
Run Code Online (Sandbox Code Playgroud)

请帮我解决这个问题.

rsb*_*rro 4

您没有. Response尝试将 设为ContentTypeimage/x-icon请注意,“正确”的内容类型可能是image/vnd.microsoft.icon,但这篇文章似乎表明您可能会遇到该类型的问题)。

代码应该类似于:

this.Page.Response.Clear();
this.Page.Response.Cache.SetCacheability(System.Web.HttpCacheability.Private);
this.Page.Response.Cache.SetExpires(DateTime.Now.AddMinutes(20));
this.Page.Response.ContentType = "image/x-icon";
Run Code Online (Sandbox Code Playgroud)