您的位置:首页 > 编程语言 > C#

C#IE图片缓存不显示

2009-06-15 17:37 113 查看
出现问题:

上传了图片,点击DataGridView某列,显示图片进行预览.使用BiraryWrite进行读取,然后放在Image控件中显示.

主要代码如下:



//用来显示图片的方法

private void showImag(string strID)
{

//三张图片,分别实例化三个BinaryWriter
BinaryWriter B_bw = null;
BinaryWriter M_bw = null;
BinaryWriter S_bw = null;
try
{
string strsql = "select B_ImageData,M_ImageData,S_ImageData from showimage where id=" + strID + "";
DataTable dt = con.GetData(strsql, "showimage");
if (dt.Rows.Count > 0)
{

//从数据库读取Image二进制数据流
Byte[] B_image = (Byte[])dt.Rows[0][0];//第一张
Byte[] M_image = (Byte[])dt.Rows[0][1];//第二张
Byte[] S_image = (Byte[])dt.Rows[0][2];//第三张

string strB_Path = "~/image/Bwangwu.jpg";//存放图片的文件夹路径.
string strM_Path = "~/image/Mwangwu.jpg";
string strS_Path = "~/image/Swangwu.jpg";



//返回与Web服务器上的指定虚拟路径相对应的物理文件路径.
string strBPath = Server.MapPath(strB_Path);
string strMPath = Server.MapPath(strM_Path);
string strSPath = Server.MapPath(strS_Path);



//实例化BinaryWriter
B_bw = new BinaryWriter(File.Open(strBPath, FileMode.Create));
M_bw = new BinaryWriter(File.Open(strMPath, FileMode.Create));
S_bw = new BinaryWriter(File.Open(strSPath, FileMode.Create));

//输出流
B_bw.Write(B_image);
M_bw.Write(M_image);
S_bw.Write(S_image);



//注意以下带参数,用作显示图片
this.B_Image.ImageUrl = strB_Path + "?dd=" + Guid.NewGuid() + "";
this.M_Image.ImageUrl = strM_Path + "?dd=" + Guid.NewGuid() + "";
this.S_Image.ImageUrl = strS_Path + "?dd=" + Guid.NewGuid() + "";

B_bw.Flush();
M_bw.Flush();
S_bw.Flush();
B_bw.Close();
M_bw.Close();
S_bw.Close();
}
}
catch(Exception ea)
{
B_bw.Close();
M_bw.Close();
S_bw.Close();
}

在本地测试没有问题.放在IIS中,服务器图片已更新,点击客户连接,显示不到最新的图片.只能按F5刷新,才能显示最新的,想到可能是IE缓存的问题.

解决方法:



第一,可以在Internet选项-> 常规->设置> 每次访问此页时检查.此时每次得到的页面都是最新的.



第二,在显示图片的路径文件中添加参数,使其每次得到的图片都是最新的.



this.S_Image.ImageUrl (Image的路径) = (从数据库中读取到的数据库文件,保存到本地的路径)strS_Path + "?dd=" + Guid.NewGuid() + ""(参数);
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: