//存
string ImgFName;
HttpPostedFile UpFile=UP_FILE.PostedFile;
FileLength=UpFile.ContentLength;//记录文件长度
try
{
if(FileLength==0)
{
cfun.alert(Response,"请选择要上传的图片");
}
else
{
Byte[] FileByteArray = new Byte[FileLength]; //图象文件临时储存Byte数组
Stream StreamObject = UpFile.InputStream; //建立数据流对像
int pos=UpFile.FileName.LastIndexOf("//");
ImgFName=UpFile.FileName.Substring(pos+1);
StreamObject.Read(FileByteArray,0,FileLength);
cmd=new SqlCommand("InsImage",con);
cmd.CommandType=CommandType.StoredProcedure;
con.Open();
cmd.Parameters.Add(new SqlParameter("@ImageData",SqlDbType.Image)).Value=FileByteArray;
cmd.Parameters.Add(new SqlParameter("@ImageName",SqlDbType.NVarChar)).Value=ImgFName;
cmd.Parameters.Add(new SqlParameter("@ImageExplain",SqlDbType.NVarChar)).Value=txtBody.Text;
cmd.Parameters.Add(new SqlParameter("@ImageGroup",SqlDbType.NVarChar)).Value=txtImgGup.Text;
cmd.Parameters.Add(new SqlParameter("@ImageDate",SqlDbType.NVarChar)).Value=labDate.Text;
cmd.ExecuteNonQuery();
cfun.alert(Response,"上传成功!","upimage.aspx");
con.Close();
}
}
catch(Exception ex)
{
cfun.alert(Response,"'"+ex.Message+"'");
}
//
读
con=new SqlConnection(ConfigurationSettings.AppSettings["ConStr"]);
SimgId=Request.QueryString["ImgId"].ToString();
Sqlstr="select ImageData from ImageStore where ImgId='"+SimgId+"'";
if(!IsPostBack)
{
con.Open();
cmd=new SqlCommand(Sqlstr,con);
dr=cmd.ExecuteReader();
while(dr.Read())
{
Response.BinaryWrite((byte[])dr["ImageData"]);
}
con.Close();
本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/Eray/archive/2007/04/01/1548505.aspx