向数据库存图片,并显示出来

ContractedBlock.gif ExpandedBlockStart.gif 代码
 
   
/* *******************************
**向数据库插入图片
*******************************
*/
string ImgPath = FileUpload1.PostedFile.FileName;
string ImgName = ImgPath.Substring(ImgPath.LastIndexOf( " \\ " ) + 1 );
string ImgExtend = ImgPath.Substring(ImgPath.LastIndexOf( " . " ) + 1 );
if ( ! (ImgExtend == " bmp " || ImgExtend == " jpg " || ImgExtend == " gif " ))
{
Response.Write(
" <script>alert(\ " 对不起,图片错误!\ " )</script> " );
}
int FileLen = this .FileUpload1.PostedFile.ContentLength;
Byte[] FileData
= new Byte[FileLen];
HttpPostedFile hp
= FileUpload1.PostedFile; // 创建访问客户端上传文件的对象
Stream sr = hp.InputStream; // 创建数据流对象
sr.Read(FileData, 0 , FileLen); // 将图片数据放到FileData数组对象实例中,其中0代表数组指针的起始位置,FileLen表示要读取流的长度(指针的结素位置)

bll.addimage(id,FileData);
// 向数据库添加



/* *************************************
*在另一个页面显示
*************************************
*/
if ( ! IsPostBack && Request.QueryString[ " id " ] != null )
{
string str = Request.QueryString[ " id " ];
Byte[] b;
if (Request.QueryString[ " id " ] != null )
{
SqlDataReader read
= bll.getimage(Request.QueryString[ " id " ]);
while (read.Read())
{
if (read[ " personimage " ] != DBNull.Value)
{
b
= ( byte [])read[ " personimage " ];
Response.ContentType
= " image/Jpeg " ; // 设定输出文件类型
Response.BinaryWrite(b);
}
}
read.Close();
}
else
{
Response.ContentType
= " image/Jpeg " ;
Response.Write(
" noperson.gif " );
}
}

 

转载于:https://www.cnblogs.com/lsq_NET/archive/2010/07/13/1775700.html

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值