Using Stream to display an * .ico image

I want to show an image with the extension *.ico , and I use Stream for this. But I have a problem.

With the extension *.jpg , *.bmp ... The image is displayed normally, but *.ico , it does not show

Here is my code:

  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); } } 

and

 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(); } } } 

Please help me solve this problem.

+6
source share
1 answer

You do not set the ContentType property to Response . Try setting the ContentType to image/x-icon (note that the โ€œcorrectโ€ type of content may be image/vnd.microsoft.icon , but this post seems to indicate that you might run into problems with this type).

The code should look something like this:

 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"; 
+4
source

Source: https://habr.com/ru/post/909713/


All Articles