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

程序中图片是动态显示的

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

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

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

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

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

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

namespace WebApplication3
{
///

1<summary>   
2/// chart 的摘要说明。   
3/// </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 只需要如下一行---------------

1 @ 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);

Published At
Categories with Web编程
Tagged with
comments powered by Disqus