2012-03-01 12 views
6

Quiero mostrar la imagen con la extensión *.ico y uso Stream para hacerlo. Pero tengo un problema.Usar Stream para visualizar la imagen * .ico

con extensión *.jpg, *.bmp ... Demostración de la imagen bien, pero *.ico, no muestra

Aquí está mi código:

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

y

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

por favor me ayude a resolver ese problema

+0

Código ve bien (extraño primordial de la seguridad SP, y no está claro por qué intenta transmitir a los archivos con la mano cuando deberían estar ya accesible por url ...) ¿Tiene algún problema para obtener la secuencia del archivo en el servidor o la representación en el navegador (no espero que ICO lo represente como etiqueta IMG) –

Respuesta

4

No está configurando la propiedad ContentType en el Response. Intente configurar el ContentType en image/x-icon (tenga en cuenta que el tipo de contenido "correcto" puede ser image/vnd.microsoft.icon, pero this post parece indicar que puede tener problemas con ese tipo).

El código debe ser algo como:

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"; 
+0

Gracias por ayudarme. Trabajé bien para mí – user1186850

+0

marca de usuario como respuesta si funciona – Dotnet

+0

@ user1186850 Me alegro de poder ayudarlo. – rsbarro

Cuestiones relacionadas