经常会遇到图片的保存这个问题,图片的保存分为两种,一是直接把图片存到数据库里存为BLOB格式,另一种是把图片上传到程序目录特定的文件夹下面,然后把路径保存起来,当然保存方式不同,展示的方式也就不同.
下面以c#的服务器控件 asp:FileUpload 为例,说一下图片保存的两种方式.
前台首页面是一个置于form表单里的一个table,有许多字段,其中图片为其中之一,页面代码如下:
<asp:FileUpload ID="ZP" runat="server" />
当页面上其它数据录入完成,图片选择上传完毕,点击保存后,方法如下:
//save
protected void Button1_Click(object sender, EventArgs e)
{
string strSQL_execute = "";
//判断数据是更新还是录入
string zjbh = Hf_zjbh.Value;
OracleConnection con = null;
OracleTransaction trans = null;
OracleCommand cmd = null;
try
{
con = OracleHelper.GetOracleConnection();//从自已定义的数据库操作类里获得当前connection
con.Open();
//启用事务1.录入(或更新)数据 2.更新已录入(或更新的)图片
trans = con.BeginTransaction();
cmd = con.CreateCommand();
cmd.Transaction = trans;
if (!string.IsNullOrEmpty(zjbh))
{
//更新
strSQL_execute = string.Format(@"update yj_zj set
zjjb ='{0}',xm='{1}',csrq=to_date('{2}','YYYY-MM-DD') where zjbh='{3}'",
zjjb.Text, XM.Text, csrq.Text, XB.Text, zjbh);
}
else
{
//插入,首先获得新的序号
cmd.CommandText = "select S_YJ_ZJ.NEXTVAL from dual";
object bh = cmd.ExecuteOracleScalar();
zjbh = bh.ToString();
strSQL_execute = string.Format(@"insert into yj_zj (zjbh, zjjb, xm,// csrq)
values('{0}','{1}','{2}',to_date('{3}','YYYY-MM-DD'))",
zjbh, zjjb.Text, xm.Text, csrq.Text);
}
cmd.CommandText = strSQL_execute;
cmd.ExecuteNonQuery();
//专家图片[此专家数据已经在上面SQL语句里录入完成]
//如果服务器的图片控件有图片
if (Request.Files.Count > 0)
{
//如果上传图片的名称不如空
if (ZP.FileName != "")
{
string zp_sql = "update yj_zj set zp = :zp where zjbh =" + zjbh;
//获得上传图片的长度
int intLen = ZP.PostedFile.ContentLength;
byte[] zp = new byte[intLen];
//读取到定义的二进度数据zp里
ZP.PostedFile.InputStream.Read(zp, 0, intLen);
//定义zp_sql语句里的blob格式的参数zp
cmd.Parameters.Add("zp", OracleType.Blob).Value = zp;
直接保存文件的方式
//string f_name = ZP.PostedFile.FileName;//获得文件名(含路径)
//string filename = Path.GetFileName(f_name);//获得文件名(不含路径)
上传文件后缀名
//string filefex = Path.GetExtension(f_name);
//Random ro = new Random();
//string sto = ro.Next(100, 100000).ToString();
定义一个新名称(时间+定义的一个随机数)
//string newName = DateTime.Now.Minute.ToString() + DateTime.Now.Second.ToString() + DateTime.Now.Millisecond.ToString() + sto;
//ZP.PostedFile.SaveAs(Server.MapPath("../UpLoad/") + newName + filefex);//将图片上传保存到指定的文件夹下
//string zp_sql= "xxxx";(更新语句,把路径"../UpLoad/" newName + filefex 保存到数据库里)
//cmd.CommandText = inSql;
//cmd.ExecuteNonQuery();
///
cmd.CommandText = zp_sql;
cmd.ExecuteNonQuery();
}
}
trans.Commit();
Response.Write("<script>alert('保存成功');location.replace(location.href)</script>");
}
catch (Exception ex)
{
if (trans != null)
{
trans.Rollback();
}
Response.Write("<script>alert('保存出现错误');location.replace(location.href)</script>");
}
finally
{
if (cmd != null)
cmd.Dispose();
if (trans != null)
trans.Dispose();
if (con != null)
con.Dispose();
}
}
展示方式:
如果图片是直接保存的路径的话, 那就在页面上定义的img标签里直接初始路径就可以了.这里不说了.
如果图片保存的是blob格式的话,下面说一种展示的方式:
在展示页面上定义一个链接指向一个单独的图片展示页面:
<img width='110px' height='130px' src=pic_fj.aspx?id=" + id + "> 其中ID为当道数据的ID
pic_fj.aspx为一个单独的展示图片的页面,获得ID的值,展示此条数据的图片,其中pic_fj.aspx页面前台什么也不用写.只需要在后台初始化即可.
后台初始方法如下:
protected void Page_Load(object sender, EventArgs e)
{
id = Request.parms["id"];
MemoryStream stream = new MemoryStream();
string sql = "select zp from yj_zj where zjbh='" + id + "'";
DataTable dt = oracle.ExecuteDataTable(sql);
Bitmap bitMap = null;
if (dt.Rows.Count > 0)
{
if (dt.Rows[0][0].ToString().Length > 0)
{
byte[] pic = (byte[])dt.Rows[0][0];
stream.Write(pic, 0, pic.Length);
bitMap = new Bitmap(stream);
}
else
{
bitMap = new Bitmap(Server.MapPath("../../Image/touxiang.jpg"));
}
}
else
{
bitMap = new Bitmap(Server.MapPath("../../Image/touxiang.jpg"));
}
Response.ContentType = "image/Jpeg";
bitMap.Save(Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg);
}
其中:MemoryStream为创建支持存储区为内在流的类.父类为System.IO;通过Bitmap类的save方法把流输出到页面上展示出来.其中Image/touxiang.jpg为一个默认为空数据的展示图片.