ASP源码.NET源码PHP源码JSP源码JAVA源码DELPHI源码PB源码VC源码VB源码Android源码

在asp.net中如何保存/取出图片到sql数据库

来源:网络整理     时间:2014-09-18     关键词:

本篇文章主要介绍了"在asp.net中如何保存/取出图片到sql数据库",对于.NETjrs看球网直播吧_低调看直播体育app软件下载_低调看体育直播感兴趣的同学可以参考一下:

一、图片存入到数据库
 
用到以下几个方面的知识:
1. 使用stream对象
2. 查找准备上传的图片的大小和类型
3.使用inputstream方法

插入图片的方法
1.表单form 的  enctype="multipart/form-data"
2. 需要一个表单来使用户选择他们要上传的文件,同时我们需要导入 system.io名称空间来处理流对象 


数据库表的设计
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)
就可以将图片输出到页面上了

三:总结

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

以上就介绍了在asp.net中如何保存/取出图片到sql数据库,包括了方面的内容,希望对.NETjrs看球网直播吧_低调看直播体育app软件下载_低调看体育直播有兴趣的朋友有所帮助。

本文网址链接:http://www.codes51.com/article/detail_296.html

相关图片

相关文章