消除图片在ie中缓存而无法更新的问题

80酷酷网    80kuku.com

  缓存|问题程序中图片是动态显示的

原先把打算把图片保存在服务器端然后显示

可是由于ie的缓存问题导致图片无法实时更新显示

所以改为把图片存在session中然后再显示

需要保存的时候再保存到本地

//--------------chart.ashx.cs-------------------

using System;
using System.Web.SessionState;
using System.IO;
using System.Web;

namespace WebApplication3
{
/// <summary>
/// chart 的摘要说明。
/// </summary>
public class ChartHandler : IHttpHandler, IReadOnlySessionState
{
public bool IsReusable
{
get { return true; }
}

public void ProcessRequest (HttpContext ctx)
{
string chartID = ctx.Request.QueryString[0];
Array arr = (Array) ctx.Session [chartID];

ctx.ClearError ();
ctx.Response.Expires = 0;
ctx.Response.Buffer = true;
ctx.Response.Clear ();

MemoryStream memStream = new MemoryStream ((byte[])arr);
memStream.WriteTo (ctx.Response.OutputStream);
memStream.Close ();

ctx.Response.ContentType = "image/gif";
ctx.Response.StatusCode = 400;
ctx.Response.End ();

}
}
}

//--------------chart.ashx 只需要如下一行---------------

<% WebHandler language="C#" class="WebApplication3.ChartHandler" codebehind="chart.ashx.cs" %>

//WebApplication3为命名空间

//ChartHandler为chart.ashx.cs中类的名字



//--------------调用说明-----------------

//需要把图片存到byte数组中 假设为byteArr 则

// ------------------------------------------------------------------------
//把图片储存在session里面
// ------------------------------------------------------------------------
HttpContext ctx = HttpContext.Current;
string chartID = Guid.NewGuid ().ToString ();

ctx.Session [chartID] = byteArr;
Image1.ImageUrl = string.Concat ("chart.ashx?", chartID);



分享到
  • 微信分享
  • 新浪微博
  • QQ好友
  • QQ空间
点击: