欢迎光临
我们一直在努力

无组件图文混合上传,MSSQL & Access

建站超值云服务器,限时71元/月

  以下代码没有规范,还有些功能没完成,有那位高手能把它修改一下,封装成类则更是造福大众,或者以后有时间我会做的。有任何错误或建议请一定要给我发e-mail:sobina@21cn.com,谢谢。
  好了,少说多做,本示例在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
%>

赞(0)
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com 特别注意:本站所有转载文章言论不代表本站观点! 本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。未经允许不得转载:IDC资讯中心 » 无组件图文混合上传,MSSQL & Access
分享到: 更多 (0)