无组件图文混合上传,MSSQL & ACCESS
类别: ASP教程
本示例在w2kServer,IIS5,SQL SERVER7中测试通过。
如有不明白的可到精华区查“图象”或“图片”关键字找到答案,或写信给我。
示例一共有三个文件:upload.htm(上传界面)
process.asp(处理程序)
showimg.asp(显示图象)
数据库:在pubs数据库中建立一个新表名为imgtest
字段名 类型 长度
-----------------------------------------------------
id int (自动编号)
img iamge(Access为OLE)
imginfo nchar 50
以下是三个文件的代码:
upload.htm
---------------------------------------------------------------------
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<style type="text/css">
<!--
td { font-size: 9pt}
a { color: #000000; text-decoration: none}
a:hover { text-decoration: underline}
.tx { height: 16px; width: 30px; border-color: black black #000000; border-top-width: 0px;
border-right-width: 0px; border-bottom-width: 1px; border-left-width: 0px; font-size: 9pt;
background-color: #eeeeee; color: #0000FF}
.bt { font-size: 9pt; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px;
border-left-width: 0px; height: 16px; width: 80px; background-color: #eeeeee; cursor: hand}
.tx1 { height: 20px; width: 30px; font-size: 9pt; border: 1px solid; border-color: black black
#000000; color: #0000FF}
-->
</style>
</head>
<body bgcolor="#FFFFFF" text="#000000">
<form name="form1" method="post" action="process.asp" enctype="multipart/form-data" >
<table width="71%" border="1" cellspacing="0" cellpadding="5" align="center"
bordercolordark="#CCCCCC" bordercolorlight="#000000">
<tr bgcolor="#CCCCCC">
<td height="22" align="left" valign="middle" bgcolor="#CCCCCC"> Sobina
的图文上传界面</td>
</tr>
<tr align="left" valign="middle" bgcolor="#eeeeee">
<td bgcolor="#eeeeee"> <br>
</td>
</tr>
<tr align="center" valign="middle">
<td align="left" id="upid" height="122">
<p>图象路径:
<input type="file" name="img" style="width:400" class="tx1" value="">
</p>
<p>图象说明:
<input type="text" name="imginfo">
</p>
</td>
</tr>
<tr align="center" valign="middle" bgcolor="#eeeeee">
<td bgcolor="#eeeeee" height="2">
<input type="submit" name="Submit" value="・ 提交 ・" class="bt">
<input type="reset" name="Submit2" value="・ 重置 ・" class="bt">
</td>
</tr>
</table>
</form>
</body>
</html>
------------------------------------------------------------------------
process.asp
------------------------------------------------------------------------
<!--#include file="../bin/strCnn.asp"-->
<%
response.expires=0
\'目的:将二进制字符转换成普通字符
Function bin2str(binstr)
Dim varlen,clow,ccc,skipflag
skipflag=0
ccc = ""
varlen=LenB(binstr)
For i=1 To varlen
If skipflag=0 Then
clow = MidB(binstr,i,1)
If AscB(clow) > 127 Then
ccc =ccc & Chr(AscW(MidB(binstr,i+1,1) & clow))
skipflag=1
Else
ccc = ccc & Chr(AscB(clow))
End If
Else
skipflag=0
End If
Next
bin2str = ccc
End Function
\'目的:把表单中的图象数据分离出来
\'其中参数formsize为表单数据大小,formdata为表单的总数据
Function ImageUp(formsize,formdata)
bncrlf=chrb(13) & chrb(10)
divider=leftb(formdata,instrb(formdata,bncrlf)-1) \'formdata第一个bncrlf左边的数据
datastart=instrb(formdata,bncrlf & bncrlf)+4 \'两个bncrlf右边的数据的起始位
dataend=instrb(datastart+1,formdata,divider)-datastart
Imageup=midb(formdata,datastart,dataend)
End Function
\'目的:把表单中的变量值取出
\'其中参数varName为要寻找的字段变量,strTXT为已从图象中分离出来的的所有文本
Function findVar(varName,strTxt)
startPos=1
strLen=len(varName)+2
\'表单中可能有多个同名变量(用在有主表与明细表中的数据更新中)
for i=1 to len(strTXT)
varStart=instr(startPos,strTXT,varName)+strLen+3
varEnd=instr(varStart,strTXT,"--")-2
varValLen=varEnd-varStart
inVar=mid(strTXT,varStart,varValLen)
findVar=findVar & inVar
startPos=instr(varStart,strTXT,varName)
if startPos=0 then exit for \'如果找不到则退出循环
findVar=findVar & "," \' 以","作为多个同名变量值间的分隔符,实际上本例倒不需要
next
End function
FormSize = Request.TotalBytes
FormData = Request.BinaryRead( FormSize )
Image = ImageUp(FormSize,Formdata)
\'以下两步不能省略,否则取不出文字
strTXT=mid(formdata,instr(formdata,image)+len(image)+1) \'取出文字
strTXT=bin2str(strTXT) \'二进制转换成普通文字
imginfo=findVar("imginfo",strTXT) \'相当于request.form("imginfo")
\'MyArray = Split(imginfo, ",", -1, 1) \'分离从imginfo返回的多个值,在本例子中不用
Application.Lock
set objCnn=Server.CreateObject("ADODB.Connection")
objCnn.Open strCnn
set rec=Server.CreateObject("ADODB.Recordset")
rec.Open "imgtest",objCnn,1,3
rec.addnew \'如果你只想更新一个记录,则可使这句失效
rec("imginfo")=imginfo
if Len(Image)>1 then
rec("img").Appendchunk Image \'把图象写入数据库
end if
rec.update
rec.close:set rec=nothing
set objCnn=Nothing
Application.Unlock
\'输出图象说明和图象
response.write imginfo
response.write "<img src=\'showimg.asp?imginfo=" & imginfo & "\' boder=0>"
%>
-----------------------------------------------------------------------
showimg.asp
-----------------------------------------------------------------------
<!--#include file="../bin/strCnn.asp"-->
<%
response.expires=0
response.buffer=true
response.clear
Response.ContentType = "image/*"
set objCnn=server.CreateObject("ADODB.connection")
objCnn.Open strCnn
strsql="SELECT * FROM imgtest where imginfo=\'" & Request.QueryString("imginfo") & "\'"
set rec=objCnn.Execute(strsql)
Response.BinaryWrite rec("img")
rec.close:set rec=nothing
set objCnn=nothing
%>
需要提醒的是: Access数据库要把显示图象的showimg.asp文件作相应调整
showimg.asp
-----------------------------------------------------------------------
<!--#include file="../bin/strCnn.asp"-->
<%
response.expires=0
response.buffer=true
response.clear
Response.ContentType = "image/bmp"
set objCnn=server.CreateObject("ADODB.connection")
objCnn.Open strCnn
strsql="SELECT * FROM imgtest where imginfo=\'" & Request.QueryString("imginfo") & "\'"
set rec=objCnn.Execute(strsql)
Dim intSize,objOle,realSize
Const cntHeader=76 \'Access数据OLE开头多了76 bytes
intize=rec("img").ActualSize
objOle=rec("img").GetChunk(cntHeader) \'76 bytes
realSize=rec("img").GetChunk(intFldSize-cntHeader) \'获取图像的真正bytes
Response.BinaryWrite relSize \'输出图象
rec.close:set rec=nothing
set objCnn=nothing
%>
如有不明白的可到精华区查“图象”或“图片”关键字找到答案,或写信给我。
示例一共有三个文件:upload.htm(上传界面)
process.asp(处理程序)
showimg.asp(显示图象)
数据库:在pubs数据库中建立一个新表名为imgtest
字段名 类型 长度
-----------------------------------------------------
id int (自动编号)
img iamge(Access为OLE)
imginfo nchar 50
以下是三个文件的代码:
upload.htm
---------------------------------------------------------------------
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<style type="text/css">
<!--
td { font-size: 9pt}
a { color: #000000; text-decoration: none}
a:hover { text-decoration: underline}
.tx { height: 16px; width: 30px; border-color: black black #000000; border-top-width: 0px;
border-right-width: 0px; border-bottom-width: 1px; border-left-width: 0px; font-size: 9pt;
background-color: #eeeeee; color: #0000FF}
.bt { font-size: 9pt; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px;
border-left-width: 0px; height: 16px; width: 80px; background-color: #eeeeee; cursor: hand}
.tx1 { height: 20px; width: 30px; font-size: 9pt; border: 1px solid; border-color: black black
#000000; color: #0000FF}
-->
</style>
</head>
<body bgcolor="#FFFFFF" text="#000000">
<form name="form1" method="post" action="process.asp" enctype="multipart/form-data" >
<table width="71%" border="1" cellspacing="0" cellpadding="5" align="center"
bordercolordark="#CCCCCC" bordercolorlight="#000000">
<tr bgcolor="#CCCCCC">
<td height="22" align="left" valign="middle" bgcolor="#CCCCCC"> Sobina
的图文上传界面</td>
</tr>
<tr align="left" valign="middle" bgcolor="#eeeeee">
<td bgcolor="#eeeeee"> <br>
</td>
</tr>
<tr align="center" valign="middle">
<td align="left" id="upid" height="122">
<p>图象路径:
<input type="file" name="img" style="width:400" class="tx1" value="">
</p>
<p>图象说明:
<input type="text" name="imginfo">
</p>
</td>
</tr>
<tr align="center" valign="middle" bgcolor="#eeeeee">
<td bgcolor="#eeeeee" height="2">
<input type="submit" name="Submit" value="・ 提交 ・" class="bt">
<input type="reset" name="Submit2" value="・ 重置 ・" class="bt">
</td>
</tr>
</table>
</form>
</body>
</html>
------------------------------------------------------------------------
process.asp
------------------------------------------------------------------------
<!--#include file="../bin/strCnn.asp"-->
<%
response.expires=0
\'目的:将二进制字符转换成普通字符
Function bin2str(binstr)
Dim varlen,clow,ccc,skipflag
skipflag=0
ccc = ""
varlen=LenB(binstr)
For i=1 To varlen
If skipflag=0 Then
clow = MidB(binstr,i,1)
If AscB(clow) > 127 Then
ccc =ccc & Chr(AscW(MidB(binstr,i+1,1) & clow))
skipflag=1
Else
ccc = ccc & Chr(AscB(clow))
End If
Else
skipflag=0
End If
Next
bin2str = ccc
End Function
\'目的:把表单中的图象数据分离出来
\'其中参数formsize为表单数据大小,formdata为表单的总数据
Function ImageUp(formsize,formdata)
bncrlf=chrb(13) & chrb(10)
divider=leftb(formdata,instrb(formdata,bncrlf)-1) \'formdata第一个bncrlf左边的数据
datastart=instrb(formdata,bncrlf & bncrlf)+4 \'两个bncrlf右边的数据的起始位
dataend=instrb(datastart+1,formdata,divider)-datastart
Imageup=midb(formdata,datastart,dataend)
End Function
\'目的:把表单中的变量值取出
\'其中参数varName为要寻找的字段变量,strTXT为已从图象中分离出来的的所有文本
Function findVar(varName,strTxt)
startPos=1
strLen=len(varName)+2
\'表单中可能有多个同名变量(用在有主表与明细表中的数据更新中)
for i=1 to len(strTXT)
varStart=instr(startPos,strTXT,varName)+strLen+3
varEnd=instr(varStart,strTXT,"--")-2
varValLen=varEnd-varStart
inVar=mid(strTXT,varStart,varValLen)
findVar=findVar & inVar
startPos=instr(varStart,strTXT,varName)
if startPos=0 then exit for \'如果找不到则退出循环
findVar=findVar & "," \' 以","作为多个同名变量值间的分隔符,实际上本例倒不需要
next
End function
FormSize = Request.TotalBytes
FormData = Request.BinaryRead( FormSize )
Image = ImageUp(FormSize,Formdata)
\'以下两步不能省略,否则取不出文字
strTXT=mid(formdata,instr(formdata,image)+len(image)+1) \'取出文字
strTXT=bin2str(strTXT) \'二进制转换成普通文字
imginfo=findVar("imginfo",strTXT) \'相当于request.form("imginfo")
\'MyArray = Split(imginfo, ",", -1, 1) \'分离从imginfo返回的多个值,在本例子中不用
Application.Lock
set objCnn=Server.CreateObject("ADODB.Connection")
objCnn.Open strCnn
set rec=Server.CreateObject("ADODB.Recordset")
rec.Open "imgtest",objCnn,1,3
rec.addnew \'如果你只想更新一个记录,则可使这句失效
rec("imginfo")=imginfo
if Len(Image)>1 then
rec("img").Appendchunk Image \'把图象写入数据库
end if
rec.update
rec.close:set rec=nothing
set objCnn=Nothing
Application.Unlock
\'输出图象说明和图象
response.write imginfo
response.write "<img src=\'showimg.asp?imginfo=" & imginfo & "\' boder=0>"
%>
-----------------------------------------------------------------------
showimg.asp
-----------------------------------------------------------------------
<!--#include file="../bin/strCnn.asp"-->
<%
response.expires=0
response.buffer=true
response.clear
Response.ContentType = "image/*"
set objCnn=server.CreateObject("ADODB.connection")
objCnn.Open strCnn
strsql="SELECT * FROM imgtest where imginfo=\'" & Request.QueryString("imginfo") & "\'"
set rec=objCnn.Execute(strsql)
Response.BinaryWrite rec("img")
rec.close:set rec=nothing
set objCnn=nothing
%>
需要提醒的是: Access数据库要把显示图象的showimg.asp文件作相应调整
showimg.asp
-----------------------------------------------------------------------
<!--#include file="../bin/strCnn.asp"-->
<%
response.expires=0
response.buffer=true
response.clear
Response.ContentType = "image/bmp"
set objCnn=server.CreateObject("ADODB.connection")
objCnn.Open strCnn
strsql="SELECT * FROM imgtest where imginfo=\'" & Request.QueryString("imginfo") & "\'"
set rec=objCnn.Execute(strsql)
Dim intSize,objOle,realSize
Const cntHeader=76 \'Access数据OLE开头多了76 bytes
intize=rec("img").ActualSize
objOle=rec("img").GetChunk(cntHeader) \'76 bytes
realSize=rec("img").GetChunk(intFldSize-cntHeader) \'获取图像的真正bytes
Response.BinaryWrite relSize \'输出图象
rec.close:set rec=nothing
set objCnn=nothing
%>
- 上一篇: ACCESS数据库表改名的一段代码
- 下一篇: 如何在生成的静态页面中显示文章被阅读的次数
-= 资 源 教 程 =-
文 章 搜 索