上传图片倒数据库:
private void BtnUpFile_Click(object sender, System.EventArgs e)
{
if("" != UpFileControl.PostedFile.FileName)
{
imagePath= UpFileControl.PostedFile.FileName;
imageType= imagePath.Substring(imagePath.LastIndexOf(".")+1);
imageName = imagePath.Substring(imagePath.LastIndexOf("\\")+1);
if("jpg" != imageType && "gif" != imageType)
{
Response.Write("<script language='javascript'> alert('对不起!请您选择jpg或者gif格式的图片!');</script>");
return;
}
else
{
HttpPostedFile SendImage=UpFileControl.PostedFile;
imageSize=SendImage.ContentLength;////取得图片
imageBody=new Byte[imageSize];
Stream StreamObject=SendImage.InputStream;//建立数据流对象
StreamObject.Read(imageBody,0,imageSize);///把图像数据放到mMarkBody中,其中0代表数据指针位置,intFileLength代表要读取的流的长度
imagePath=UpFileControl.PostedFile.FileName;//取得图象路径
SqlCom="insert into Image(ImageName,Image,ImageSize) values('"+imageName+"','"+imageBody+"','"+imageSize.ToString()+"')";
con.Open();
cmd = new SqlCommand(SqlCom,con);
try
{
cmd.ExecuteNonQuery();
Response.Write("成功!");
}
catch
{
Response.Write("失败!");
}
con.Close();
}
}
}
从数据库读出图片
private void btnDown_Click(object sender, System.EventArgs e)
{
SqlCom="select top 1 image,imageName from image order by ID desc";
cmd=new SqlCommand(SqlCom,con);
con.Open();
SqlDataReader myReader=cmd.ExecuteReader();
try
{
myReader.Read();
}
catch
{
Response.Write("出现异常!");
}
Response.Buffer=true;
Response.Clear();
Response.ContentType="gif";
Response.AddHeader("Content-Disposition","attachment;filename="+myReader["imageName"].ToString() + ";");
byte[] file = (byte[])myReader["Image"];
Response.BinaryWrite(file);
Response.Flush();
Response.End();
con.Close();
myReader.Close();
}
posted on 2006-04-28 16:38
萌芽的叶子 阅读(137)
评论(0) 编辑 收藏 引用 所属分类:
asp.net