imports system.data.sqlclient public class webform1 inherits system.web.ui.page '窗体代码省略 '上传图片 private sub button1_click(byval sender as system.object, byval e as system.eventargs) handles button1.click
dim img as string '定义postedfile文件是储存用户上载的文件 dim postedfile as httppostedfile = file1.postedfile '定义一个变量储存用户上载文件的大小 dim intimgsize as int32 '获取用户上传文件的大小, intimgsize = postedfile.contentlength
'如果要上传的文件不为空 if intimgsize <> 0 then
'如果大于8k, 则禁止上传 if intimgsize > 8000 then label1.text = "图片太大" exit sub end if
'定义一个变量储存用户上传图片的文件类型 dim strimgtype as string = postedfile.contenttype
'只接受.gif格式的图片 dim filesplit() as string = split(strimgtype, "/") strimgtype = filesplit(filesplit.length - 1) if strimgtype <> "gif" then label1.text = "图片格式不对" exit sub end if
'储存要上传的文件的整个路径 filesplit = split(postedfile.filename, "/") '取得上传文件的文件名 dim filename as string = filesplit(filesplit.length - 1) '将上传的图片保存到服务器当前目录的headimg文件夹中 postedfile.saveas(server.mappath("headimg") & "/" & filename) '定义一个变量储存服务器上当前上传图片的路径 dim imgpath as string = "headimg/" & filename img = "<img src=" & imgpath & " border=0>"
'将图片储存到数据库 dim scon as new sqlconnection("server=localhost;database=northwind;uid=sa;pwd=123") scon.open() dim scom as new sqlcommand("insert into users values (@img)", scon) scom.parameters.add("@img", sqldbtype.varchar).value = img try scom.executenonquery() catch ex as exception end try scon.close() '转到查看图片窗口 response.redirect("viewpicture.aspx") end if end sub end class 2、viewpicture.aspx代码:
imports system.data.sqlclient public class viewpicture inherits system.web.ui.page ‘窗体代码省略 private sub page_load(byval sender as system.object, byval e as system.eventargs) handles mybase.load dim scon as new sqlconnection("server=localhost;database=northwind;uid=sa;pwd=123") dim sda as new sqldataadapter("select * from users", scon) dim ds as new dataset try sda.fill(ds) catch ex as exception end try datagrid1.datasource = ds datagrid1.databind() end sub end class注册会员,创建你的web开发资料库,