图片存放在数据中的问题? 我把图片存放在数据库(sql 2k)中,然后读出来在硬盘上先生成一个图片文件,用Image控件显示,一切正常、但当我编辑(换成别的图片)后再保存到数据中后,再读出来,还是用Image控件显示,这时显示的是旧图片,而且数据库中正存的是新图片,硬盘上也是新图片文件,但Image控件显示就是旧图片,大家有没碰到过这样的问题啊?各位大虾帮帮忙啊…… 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 看看你得Image綁定圖片有沒有問題 Image控件没有删除原来的图片吧?调试跟踪以下就明白! to lampson123(微软): 这两种方式我都用,根据不同的需求,我用不同的方法写! 这是老板的意思,我也没有办法啊!to dragonfly001(我思考,我生存!) 我的图片绑定没有问题,我在它属性中取它的路径,然后在IE中打开 一切正常,然后我再回我那页面,这时显示就正常了,就是说我如果 没有在IE中打开过的,显示总是不对的,一打开后,那边也就正常了 to JasonHeung(拥有一切也不过就这样笑着哭): 我一开始就注意到这一点,所以我用了以下代码:private void Image_Source_Load(object sender, System.EventArgs e){ if ((string)ViewState["FileName"] != string.Empty && ViewState["FileName"] != null ) File.Delete((string)ViewState["FileName"]);} 下面是代码:private void DataGrid1_ItemCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e){ string FSQL, FileName, AreaFileName ; if (e.CommandName.Equals("Edit")) { TableCell IDCell = e.Item.Cells[0] ; ViewState["PicID"] = IDCell.Text.Trim() ; SqlConnection sqlConn = new SqlConnection() ; sqlConn.ConnectionString = (string)ViewState["ItemConnTxt"] ; sqlConn.Open() ; FSQL = "select * from t_mdlpic where id = "+IDCell.Text.Trim() ; SqlDataAdapter SQLGetMDLPic = new SqlDataAdapter(FSQL,sqlConn) ; DataSet DSGetMDLPic = new DataSet() ; SQLGetMDLPic.Fill(DSGetMDLPic,"t_mdlpic") ; if (DSGetMDLPic.Tables[0].Rows.Count >0) { TBPicNo.Text = DSGetMDLPic.Tables[0].Rows[0]["picno"].ToString().Trim() ; TBFileName.Text = DSGetMDLPic.Tables[0].Rows[0]["picname"].ToString().Trim() ; if(DSGetMDLPic.Tables[0].Rows[0]["picture"] is byte[]) { byte[] Imageby=(byte[])DSGetMDLPic.Tables[0].Rows[0]["picture"]; MemoryStream MSPic =new MemoryStream(Imageby) ; AreaFileName =DSGetMDLPic.Tables[0].Rows[0]["code"].ToString().Trim()+ DSGetMDLPic.Tables[0].Rows[0]["modelid"].ToString().Trim()+ DSGetMDLPic.Tables[0].Rows[0]["picno"].ToString().Trim()+ "."+DSGetMDLPic.Tables[0].Rows[0]["pictype"].ToString().Trim() ; FileName = MapPath("").Trim()+"/ItemModelPic/"+AreaFileName.Trim() ; FileStream FSPic = new FileStream(FileName,FileMode.Create); FSPic.Read(Imageby,0,Convert.ToInt32(MSPic.Length)); FSPic.Write(Imageby,0,Convert.ToInt32(MSPic.Length)); FSPic.Close(); Image_Source.ImageUrl = "ModelPic/"+AreaFileName.Trim() ; ViewState["FileName"] = FileName ; } } }} 下面是代码:private void DataGrid1_ItemCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e){ string FSQL, FileName, AreaFileName ; if (e.CommandName.Equals("Edit")) { TableCell IDCell = e.Item.Cells[0] ; SqlConnection sqlConn = new SqlConnection() ; sqlConn.ConnectionString = (string)ViewState["ItemConnTxt"] ; sqlConn.Open() ; FSQL = "select * from t_mdlpic where id = "+IDCell.Text.Trim() ; SqlDataAdapter SQLGetMDLPic = new SqlDataAdapter(FSQL,sqlConn) ; DataSet DSGetMDLPic = new DataSet() ; SQLGetMDLPic.Fill(DSGetMDLPic,"t_mdlpic") ; if (DSGetMDLPic.Tables[0].Rows.Count >0) { TBPicNo.Text = DSGetMDLPic.Tables[0].Rows[0]["picno"].ToString().Trim() ; TBFileName.Text = DSGetMDLPic.Tables[0].Rows[0]["picname"].ToString().Trim() ; if(DSGetMDLPic.Tables[0].Rows[0]["picture"] is byte[]) { byte[] Imageby=(byte[])DSGetMDLPic.Tables[0].Rows[0]["picture"]; MemoryStream MSPic =new MemoryStream(Imageby) ; AreaFileName =DSGetMDLPic.Tables[0].Rows[0]["code"].ToString().Trim()+ DSGetMDLPic.Tables[0].Rows[0]["modelid"].ToString().Trim()+ DSGetMDLPic.Tables[0].Rows[0]["picno"].ToString().Trim()+ "."+DSGetMDLPic.Tables[0].Rows[0]["pictype"].ToString().Trim() ; FileName = MapPath("").Trim()+"/ModelPic/"+AreaFileName.Trim() ; FileStream FSPic = new FileStream(FileName,FileMode.Create); FSPic.Read(Imageby,0,Convert.ToInt32(MSPic.Length)); FSPic.Write(Imageby,0,Convert.ToInt32(MSPic.Length)); FSPic.Close(); Image_Source.ImageUrl = "ModelPic/"+AreaFileName.Trim() ; ViewState["FileName"] = FileName ; } } }} 可以动态生成Image 控件再绑定 每次都从DB读取应该不会显示旧的图片.你是在DataGrid的ItemDataBind中给img添加URL的吗? to cjzlxy(享受孤独): 没有啊! to ybbigepl() : 我也试了,还是没有用!to lampson123(微软): 动态我怕会影响速度啊 其实你这样的性能不是很好的,我用惯另外一种输出:在Page里放入:<asp:Image ID="imgLogo" ImageUrl = "img.aspx"; Runat="server" ></asp:Image>然后定义img.aspx页面的输出:Response.Clear();Response.ContentType="image/gif";Response.AppendHeader("Content-Disposition","filename=Logo"+System.DateTime.Now.Ticks.ToString()+".jpg"); //下面是你读取的数据库流的输出Response.BinaryWrite(logo);Response.End();愚见!! to kanbozhang(kanbo.z.): 谢谢!高招!结贴! 请问这种现象怎么解释,很不理解,有关static 请教一下 微软提供的范例太老了! 高分求助桌面图标自动排列 每天12点提示吃饭 这种功能怎么实现呢?急等!!!希望各位大哥给予指导!!! 怎么样把一个表里面的二进制数据读出来再插入到另一个表中???? 求助~~~~快啊~~~~时时在线~~ 帮我看看参数查询的问题,谢了! Win8 Metro开发 如何调用平板摄像头功能? 请问.net类库下的XmlNode和XmlElement有什么区别? 如何调用在资源管理器空白处右击弹出的菜单
这两种方式我都用,根据不同的需求,我用不同的方法写!
这是老板的意思,我也没有办法啊!to dragonfly001(我思考,我生存!)
我的图片绑定没有问题,我在它属性中取它的路径,然后在IE中打开
一切正常,然后我再回我那页面,这时显示就正常了,就是说我如果
没有在IE中打开过的,显示总是不对的,一打开后,那边也就正常了
我一开始就注意到这一点,所以我用了以下代码:
private void Image_Source_Load(object sender, System.EventArgs e)
{
if ((string)ViewState["FileName"] != string.Empty &&
ViewState["FileName"] != null )
File.Delete((string)ViewState["FileName"]);
}
System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
string FSQL, FileName, AreaFileName ;
if (e.CommandName.Equals("Edit"))
{
TableCell IDCell = e.Item.Cells[0] ;
ViewState["PicID"] = IDCell.Text.Trim() ; SqlConnection sqlConn = new SqlConnection() ;
sqlConn.ConnectionString = (string)ViewState["ItemConnTxt"] ;
sqlConn.Open() ;
FSQL = "select * from t_mdlpic where id = "+IDCell.Text.Trim() ;
SqlDataAdapter SQLGetMDLPic = new SqlDataAdapter(FSQL,sqlConn) ;
DataSet DSGetMDLPic = new DataSet() ;
SQLGetMDLPic.Fill(DSGetMDLPic,"t_mdlpic") ;
if (DSGetMDLPic.Tables[0].Rows.Count >0)
{
TBPicNo.Text = DSGetMDLPic.Tables[0].Rows[0]["picno"].ToString().Trim() ;
TBFileName.Text = DSGetMDLPic.Tables[0].Rows[0]["picname"].ToString().Trim() ;
if(DSGetMDLPic.Tables[0].Rows[0]["picture"] is byte[])
{
byte[] Imageby=(byte[])DSGetMDLPic.Tables[0].Rows[0]["picture"];
MemoryStream MSPic =new MemoryStream(Imageby) ;
AreaFileName =DSGetMDLPic.Tables[0].Rows[0]["code"].ToString().Trim()+
DSGetMDLPic.Tables[0].Rows[0]["modelid"].ToString().Trim()+
DSGetMDLPic.Tables[0].Rows[0]["picno"].ToString().Trim()+
"."+DSGetMDLPic.Tables[0].Rows[0]["pictype"].ToString().Trim() ;
FileName = MapPath("").Trim()+"/ItemModelPic/"+AreaFileName.Trim() ;
FileStream FSPic = new FileStream(FileName,FileMode.Create);
FSPic.Read(Imageby,0,Convert.ToInt32(MSPic.Length));
FSPic.Write(Imageby,0,Convert.ToInt32(MSPic.Length));
FSPic.Close();
Image_Source.ImageUrl = "ModelPic/"+AreaFileName.Trim() ;
ViewState["FileName"] = FileName ;
}
}
}
}
System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
string FSQL, FileName, AreaFileName ;
if (e.CommandName.Equals("Edit"))
{
TableCell IDCell = e.Item.Cells[0] ;
SqlConnection sqlConn = new SqlConnection() ;
sqlConn.ConnectionString = (string)ViewState["ItemConnTxt"] ;
sqlConn.Open() ; FSQL = "select * from t_mdlpic where id = "+IDCell.Text.Trim() ;
SqlDataAdapter SQLGetMDLPic = new SqlDataAdapter(FSQL,sqlConn) ;
DataSet DSGetMDLPic = new DataSet() ;
SQLGetMDLPic.Fill(DSGetMDLPic,"t_mdlpic") ;
if (DSGetMDLPic.Tables[0].Rows.Count >0)
{
TBPicNo.Text = DSGetMDLPic.Tables[0].Rows[0]["picno"].ToString().Trim() ;
TBFileName.Text = DSGetMDLPic.Tables[0].Rows[0]["picname"].ToString().Trim() ;
if(DSGetMDLPic.Tables[0].Rows[0]["picture"] is byte[])
{
byte[] Imageby=(byte[])DSGetMDLPic.Tables[0].Rows[0]["picture"];
MemoryStream MSPic =new MemoryStream(Imageby) ;
AreaFileName =DSGetMDLPic.Tables[0].Rows[0]["code"].ToString().Trim()+
DSGetMDLPic.Tables[0].Rows[0]["modelid"].ToString().Trim()+
DSGetMDLPic.Tables[0].Rows[0]["picno"].ToString().Trim()+
"."+DSGetMDLPic.Tables[0].Rows[0]["pictype"].ToString().Trim() ;
FileName = MapPath("").Trim()+"/ModelPic/"+AreaFileName.Trim() ;
FileStream FSPic = new FileStream(FileName,FileMode.Create);
FSPic.Read(Imageby,0,Convert.ToInt32(MSPic.Length));
FSPic.Write(Imageby,0,Convert.ToInt32(MSPic.Length));
FSPic.Close();
Image_Source.ImageUrl = "ModelPic/"+AreaFileName.Trim() ;
ViewState["FileName"] = FileName ;
}
}
}
}
你是在DataGrid的ItemDataBind中给img添加URL的吗?
没有啊!
to lampson123(微软): 动态我怕会影响速度啊
在Page里放入:
<asp:Image ID="imgLogo" ImageUrl = "img.aspx"; Runat="server" ></asp:Image>
然后定义img.aspx页面的输出:
Response.Clear();
Response.ContentType="image/gif";
Response.AppendHeader("Content-Disposition","filename=Logo"+System.DateTime.Now.Ticks.ToString()+".jpg");
//下面是你读取的数据库流的输出
Response.BinaryWrite(logo);
Response.End();愚见!!