一、把图片存入数据库中
用到以下几个方面的知识:
1. 使用流对象
2. 查找准备上传的图片的大小和类型
3.怎么使用inputstream方法
插入图片的必要条件
1.#form 标记的 enctype 属性应该设置成 enctype="multipart/form-data"
2.# 需要一个<input type=file>表单来使用户选择他们要上传的文件,同时我们需要导入 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("<script>alert('图片类型为""')</script>") '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("<script>alert('图片不得大于k')</script>")
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的字段就行了。
Java Asp PHP .Net XML C/C++ CGI VB Jsp J2ee J2se J2me EJB Servlet Tomcat Resin Struts Weblogic Eclipse ANT GUI JMS Web servise IDEA Webphere Hibernate Spring Jboss Applet Swing Socket Javamail Perl Ajax P2P 安全 模式 框架 测试 开源 游戏
Windows XP Windows 2000 Windows 2003 Windows Me Windows 9.x Linux UNIX 注册表 操作系统 服务器 应用服务器