本文来源于网页设计爱好者web开发社区http://www.html.org.cn收集整理,欢迎访问。changimage.aspx中代码:
<%@ page language="vb" autoeventwireup="false" codebehind="changimage.aspx.vb" inherits="uploadimage.changimage"%>
<!doctype html public "-//w3c//dtd html 4.0 transitional//en">
<html>
<head>
<title></title>
<meta name="generator" content="microsoft visual studio.net 7.0">
<meta name="code_language" content="visual basic 7.0">
<meta name="vs_defaultclientscript" content="javascript">
<meta name="vs_targetschema" content="http://schemas.microsoft.com/intellisense/ie5">
</head>
<body ms_positioning="gridlayout">
<form id="form1" method="post" enctype="multipart/form-data" runat="server">
<font face="宋体"><input id="file1" type="file" size="10" name="file1" runat="server"> <asp:button id="cmdupload" runat="server" text="上传图片" width="81px" height="42px"></asp:button>
</font>
</form>
</body>
</html>
changimage.aspx.vb中代码如下:
public class changimage
inherits system.web.ui.page
protected withevents cmddemo as system.web.ui.webcontrols.button
protected withevents cmdupload as system.web.ui.webcontrols.button
protected withevents sqlconn as system.data.sqlclient.sqlconnection
protected withevents sqlcomm as system.data.sqlclient.sqlcommand
protected withevents file1 as system.web.ui.htmlcontrols.htmlinputfile #region " web form designer generated code " 'this call is required by the web form designer.
<system.diagnostics.debuggerstepthrough()> private sub initializecomponent()
me.sqlconn = new system.data.sqlclient.sqlconnection()
me.sqlcomm = new system.data.sqlclient.sqlcommand() end sub private sub page_init(byval sender as system.object, byval e as system.eventargs) handles mybase.init
form designer
initializecomponent()
end sub #end region private sub page_load(byval sender as system.object, byval e as system.eventargs) handles mybase.load
end sub private sub cmdupload_click(byval sender as system.object, byval e as system.eventargs) handles cmdupload.click
dim image as system.drawing.image, newimage as system.drawing.image
dim callb as system.drawing.image.getthumbnailimageabort
dim f as system.io.file, fs as system.io.filestream
dim temppath as string
dim bigdata as byte(), smalldata as byte() '大图片数据、小图片数据
dim pic as system.data.sqlclient.sqlparameter, picsmall as system.data.sqlclient.sqlparameter
'检察上传文件是否合标准,check函数是我根据网站需要写的了
if check(file1.postedfile.filename) <> "ok" then
response.write(check(file1.postedfile.filename))
exit sub
end if
'设置临时路径,为了防止多用户访问时的冲突,设了一个application对象
if application("image") = "" then
application("image") = 0
end if
application.lock()
temppath = server.mappath(cstr(application("image"))) '临时路径
application("image") = application("image") + 1
application.unlock()
'读取图片的数据
redim bigdata((me.file1.postedfile.inputstream.length)
me.file1.postedfile.inputstream.read(bigdata, 0, ubound(bigdata)) '将原图片数据读到bigdata中
'改变图片的大小
image = system.drawing.image.fromstream(me.file1.postedfile.inputstream)
'newimage里面的size也可另外设置,我只用了80*60和60*80两种
if image.width > image.height then
newimage = image.getthumbnailimage(80, 60, callb, new system.intptr(0))
else
newimage = image.getthumbnailimage(60, 80, callb, new system.intptr(0))
end if
image.dispose()
'将新图片及图片变小后存到临时路径中
newimage.save(temppath, system.drawing.imaging.imageformat.jpeg)
newimage.dispose()
'读取临时文件数据到smalldata中
fs = new system.io.filestream(temppath, io.filemode.open, io.fileaccess.read)
redim smalldata(fs.length)
fs.read(smalldata, 0, ubound(smalldata))
fs.close()
'上述获得小图片的方法我原本想用system.io.memorystream的,可是行不通:代码如下:
'dim m as system.io.memorystream
'm=new system.io.memorystream()
'newimage.save(m,system.drawing.imaging.imageformat.jpeg)
'redim smalldata(m.length)
'm.read(smalldata,0,m.length)
'可是上述方法读出来的smalldata全是空的,不知道原因,请指教
'删除临时文件
if f.exists(temppath) then
f.delete(temppath)
end if
'将数据加入数据库中
'由于数据库中有image字段,我用sql插不进去,就用一个存储过程
'请教各位大虾用sql语句插入有image字段的表该怎么写
'用insert into talbe(pic,picsmall) values("&bigdata&","&smalldata&")这样不行,用'"&bigdata&"'也不行呀!
sqlconn = new system.data.sqlclient.sqlconnection(connstr) '可自己设置connstr连接数据库服务器
sqlcomm = new system.data.sqlclient.sqlcommand()
sqlcomm.commandtype = commandtype.storedprocedure
sqlcomm.commandtext = "dbo.image"
pic = new system.data.sqlclient.sqlparameter("@pic", sqldbtype.image)
pic.value = bigdata
picsmall = new system.data.sqlclient.sqlparameter("@picsmall", sqldbtype.image)
picsmall.value = smalldata
sqlcomm.parameters.add(pic)
sqlcomm.parameters.add(picsmall)
sqlcomm.connection = sqlconn
sqlcomm.connection.open()
sqlcomm.executenonquery()
sqlcomm.connection.close()
sqlcomm.dispose()
sqlconn.dispose()
end sub
end class dbo.image存储过程如下:
create proc dbo.image
@pic image,
@picsmall image
as
insert into table(pic,picsmall) values (@pic,@picsmall)