求ASP.NET高手指教一下,如何把图片路径写入数据库,并在前台显示出来,小弟初学!

2025-01-06 01:54:26
推荐回答(1个)
回答1:

private void BtnOk1_Click(object sender, System.EventArgs e)
{
this.Label_picindex.Text = "";
this.Label_Help.Text = "";
this.Label_cata.Text = "";
this.Label_IndexImgUrl.Text = "";
this.Text_PicCode.Text = "";
this.Text_PicItemNum.Text = "1";
this.Label_ItemHelp.Text = "";
this.Text_Key.Value = "";

string xfile = this.File_picindex.PostedFile.FileName;

if (xfile=="")
{
this.Label_picindex.Text = "请选择一个图片";
return;
}
string fex = System.IO.Path.GetExtension( xfile ).ToLower();

if ( fex != ".gif" && fex != ".jpg" && fex != ".jpeg" && fex != ".bmp" && fex != ".png" )
{
this.Label_Help.Text = "不是合法的图片格式!";
return;
}

this.Label_cata.Text = "";

//首先取得最大的号码,然后保存图片,再向主表Pic插入数据,
//
string sql = "select max( PicCode ) from Pic";
string MaxCode = Convert.ToString( DBUtil.ExecuteScalar( sql ) );
string NextCode = Util.GetNextCode( MaxCode,"0",8 );
//this.Label_Help.Text = NextCode;

string lv_PicCode = NextCode; //PicCode
string lv_PicTitle = this.Text_PicTitle.Text; //PicTitle
string lv_PicSpec = this.Text_PicSpec.Text; //PicSpec
string lv_CataCode = this.Hid_ColCode.Value; //CataCode
string lv_Index = this.Text_Index.Value; //Index
string lv_iswallpaper = "0";
long lv_setgooddate = System.DateTime.Now.Ticks;

this.Text_PicCode.Text = NextCode;

if ( this.CheckBox_iswallpaper.Checked )
{
lv_iswallpaper = "1";
}
else
{
lv_iswallpaper = "0";
}

if ( this.CheckBox_SetGood.Checked )
{
lv_setgooddate = System.DateTime.Now.Ticks;

}
else
{
lv_setgooddate = 0;
}

if (lv_PicTitle == null || lv_PicTitle == "")
{
this.Label_Help.Text = "请输入标题";
return;
}
if ( lv_CataCode==null || lv_CataCode=="" )
{
this.Label_cata.Text = "请选择一个类别";
return;
}

//////////////////////////////////////////////////////////////////////////////////////////////
///上传索引图片

string IndexFolderString = System.Configuration.ConfigurationSettings.AppSettings["IndexFolder"];
string RootPath = Server.MapPath("/");
string DateString = System.DateTime.Now.ToString("yyyyMMdd");
string IndexFolder = RootPath + IndexFolderString + "\\" + DateString;

System.IO.DirectoryInfo dir = new System.IO.DirectoryInfo( IndexFolder );
if ( !dir.Exists )
{
dir.Create();
}
//this.File_picindex.po

string IndexImgFileName = CncFile.GetImgFileName(NextCode,0,this.File_picindex.PostedFile);

string FullName = IndexFolder + "\\" + IndexImgFileName;
string oFullName = IndexFolder + "\\o_" + IndexImgFileName;
string IndexImgUrl = "\\" + IndexFolderString + "\\" + DateString + "\\" + IndexImgFileName;
string oIndexImgUrl = "\\" + IndexFolderString + "\\" + DateString + "\\o_" + IndexImgFileName;

//////////////////////////////////////////////////////////////////////////////////////////////

string strSql = "Insert Into Pic(PicCode,PicTitle,PicSpec,CataCode,IndexImgUrl,IndexNo,iswallpaper,SetGoodDate) Values (@PicCode,@PicTitle,@PicSpec,@CataCode,@IndexImgUrl,@IndexNo,@iswallpaper,@setgooddate)";

SqlParameter[] parms =new SqlParameter[8];
parms[0]=new SqlParameter("@PicCode",lv_PicCode);
parms[1]=new SqlParameter("@PicTitle",lv_PicTitle);
parms[2]=new SqlParameter("@PicSpec",lv_PicSpec);
parms[3]=new SqlParameter("@CataCode",lv_CataCode);
parms[4]=new SqlParameter("@IndexImgUrl",IndexImgUrl);
parms[5]=new SqlParameter("@IndexNo",lv_Index);
parms[6]=new SqlParameter("@iswallpaper",lv_iswallpaper);
parms[7]=new SqlParameter("@setgooddate",lv_iswallpaper);

try
{
//存组的原图
this.File_picindex.PostedFile.SaveAs( oFullName );
if ( this.CheckBox_indexsmall.Checked )
{
//存组的缩略图
if (lv_iswallpaper == "1")
{
cnclib.Img.ImgUtil.MakeSmall(oFullName,FullName,160,120,"W");
}
else
{
cnclib.Img.ImgUtil.MakeSmall(oFullName,FullName,110,83,"W");
}
}
else
{
this.File_picindex.PostedFile.SaveAs( FullName );
}

this.Label_IndexImgUrl.Text = "点击查看图片";
this.Image_Index.ImageUrl = IndexImgUrl;

DBUtil.ExecuteNoQuery(strSql,parms);
this.Label_Help.Text="添加信息成功,请继续添加图片";

this.SubPanel.Visible = true;
this.BtnOk1.Enabled = false;
this.BtnNext.Enabled = true;
this.Text_PicTitle.Enabled = false;
this.Text_PicSpec.Enabled = false;

this.Label_PicItemTitle.Text = "给[" + lv_PicTitle + "]添加图片";

this.Label_PicItemTitle.Visible = true;
this.Datagrid_PicItem.Visible = true;

}
catch(SqlException ex)
{
this.Label_Help.Text="添加信息失败 " + ex.ToString();
return;
}
finally
{
//finally操作
}
//
string HotKey = Convert.ToString( DBUtil.ExecuteScalar( "select HotKey from CataInfo where CataCode='" + lv_CataCode + "'" ) );

this.Label_Key.Text = Ma.HotKeySelect( HotKey );

this.PicItemDataBind();
}