All articles| All Pictures| All Softwares| All Video| Go home page| Write articles| Upload pictures

Reading number is top 10 articles
HTML 表格特效整理_[Html教程]
用php读取xml数据_php资料_编程技术
使用PHP通过SMTP发送邮件新手指南_php资料_编程技术
ajax+asp.net2.0,sql2005三级联下拉框_[Asp.Net教程]
ADO.NET的一些使用经验_[Asp.Net教程]
构建安全的Xml,Web,Service系列(一)_[Asp.Net教程]
解决SQL Server导入数据时id自增属性丢失_[SQL Server教程]
交易失败[MSSQL],SQL,Server,不存在或访问被拒绝。_mssql学习_编程技术
使用PHP脚本修改Linux或Unix系统口令_php资料_编程技术
学习PHP小结:PHP编程中常用的三则技巧_[PHP教程]
Reading number is top 10 pictures
The beginning and end
The cat shit
Extremely rare TianShan Mountains snow lotus2
Compared GDP and per capita income in China for 40 years
这只猪到底犯了什么错?
擦地板的大叔太好了
战场废物1
西班牙山村小景4
Fan bingbing black wings for platform and DanLuoWang believes beauty1
Chinese paper-cut grilles art appreciation7
Download software ranking
Twenty piece of palm leaf
变速齿轮3.26
Boxer vs Yellow4
WebService在.NET中的实战应用教学视频 → 第4集
仙剑奇侠传98版歌曲
Sora aoi‘s film--Lust fan wall
C#编程思想
Unix video tutorial10
c#程序设计案例教程
Tram sex maniac 2 (H) rar bag9
delv published in(发表于) 2014/1/6 9:11:05 Edit(编辑)
在ASP.net中从SQL数据库保存取出图片(可用于上传图片)

在ASP.net中从SQL数据库保存取出图片(可用于上传图片)

在ASP.net中保存/取出图片入/从SQL数据库(可用于上传图片)_[Asp.Net教程]























一、把图片存入数据库中

用到以下几个方面的知识:
1. 使用流对象
2. 查找准备上传的图片的大小和类型
3.怎么使用InputStream方法




插入图片的必要条件
1.#Form 标记的 enctype 属性应该设置成 enctype="multipart/form-data"
2.# 需要一个表单来使用户选择他们要上传的文件,同时我们需要导入 System.IO名称空间来处理流对象
对SqlServer做以下的准备
1.# 需要至少含有一个图片类型的字段的表
2.# 如果我们还有另外一个变字符类型的字段来存储图片类型,那样会更好一些。




窗体控件
1.插入图片用到的是System.Web.UI.HtmlControls.HtmlInputFile控件,我们在webform中放入这个控件,取名为“imgInput”
2.同时再放入一个确认上传按钮“Button1”




程序代码
AddImg,用于返回要上传的图片内容




1Private Function AddImg()Function AddImg(ByVal InputImg As System.Web.UI.HtmlControls.HtmlInputFile, ByVal ImgType As String, ByVal MaxSize As Int64) As Byte()
2'传入一个htmlinputfile控件,一个上传图片格式和一个上传图片最大值,返回图片的内容,既要写入数据库中的内容,你也可以同时写入图片类型
3 Dim intImageSize As Int64
4 Dim strImageType As String
5 Dim ImageStream As Stream
6 ' Gets the Image Type
7 strImageType=InputImg.PostedFile.ContentType
8 If strImageType <> ImgType Then
9 Response.Write("") 'jgp类型为"image/pjpeg"
10 Exit Function
11 End If
12 ' Gets the Size of the Image
13 intImageSize = InputImg.PostedFile.ContentLength
14 If intImageSize > MaxSize Then
15 Response.Write("")
16 Exit Function
17 End If
18 ' Reads the Image
19 ImageStream = InputImg.PostedFile.InputStream
20 Dim ImageContent(intImageSize) As Byte
21 Dim intStatus As Integer
22 intStatus = ImageStream.Read(ImageContent, 0, intImageSize)
23 Return ImageContent
24 End Function
示例调用




Dim imageContent() As Byte
imageContent = AddImg(fileImg, "image/pjpeg", 512000)'上传图片类型为jpg,最大不超过500K




插入数据库




我想这部分就不用写了吧,你可以用任何方式(推荐使用存储过程),将imageContent插入到数据库中类型为image的字段就行了。




二、把图片从数据库中读出




这部分比较简单:




假设img变量是你从数据库中取出的图片内容
那么直接使用
Response.BinaryWrite(img)
就可以将图片输出到页面上了




三:总结




将图片存放在数据库中其实是起到了图片保护的作用,这样就算别人浏览你的机器也看不到你的图片,也可以用来保护重要的图片资料。




来源:网络












































添加到del.icio.us 添加到新浪ViVi 添加到百度搜藏 添加到POCO网摘 添加到天天网摘365Key 添加到和讯网摘 添加到天极网摘 添加到黑米书签 添加到QQ书签 添加到雅虎收藏 添加到奇客发现 diigo it 添加到饭否 添加到飞豆订阅 添加到抓虾收藏 添加到鲜果订阅 digg it 貼到funP 添加到有道阅读 Live Favorites 添加到Newsvine 打印本页 用Email发送本页 在Facebook上分享


Disclaimer Privacy Policy About us Site Map

If you have any requirements, please contact webmaster。(如果有什么要求,请联系站长)
Copyright ©2011-
uuhomepage.com, Inc. All rights reserved.