ITPub博客

首页 > 数据库 > 数据库开发技术 > 单个图片上传到数据库 (转)

单个图片上传到数据库 (转)

原创 数据库开发技术 作者:gugu99 时间:2007-08-15 14:11:42 0 删除 编辑
单个图片上传到数据库 (转)[@more@]

第一重:单个图片上传到数据库

  这个问题已经不是什么新鲜问题了,网上也有大把的教程,但大多数是授人以鱼,而不授人以渔,经过辛苦的资料收集,思考,调试,整理,我基本上已经把这个问题从原理上搞清楚了,现在根据我自己的理解,在范例程序的基础上,加以解释,希望能对部分网友(比我还菜的:-))有所帮助。

  请诸位大虾能对其中的不正或不良这处予以指正。

  程序中stream对象的用法上参考了“化境HTTP上传程序 Version 2.0”在代码,在此对稻香老农和梁无惧表示衷心的感谢和由衷的敬意。

  我想循序渐进,今天先讲一个简单的,单个图片文件保存到数据库。

这个范例共包括三个ASP文件和一个数据库(一个表),全部在同一目录下。

1、tblImage 表结构(ACCESS 2000)

  sn     自动编号 序列号
  content-type 文本   图片类型
  image    OLE 对象 图片数据

2、SimpleImageToData.ASP:上传表单及保存图片到数据库的代码部分,主要文件。

<%@ Language=vbscript %>
<% option explicit %>

<%
 '从一个完整路径中析出文件名称
 function getFileNamefromPath(strPath)
 getFileNamefromPath = mid(strPath,instrrev(strPath,"")+1)
 end function

 '定义数据库连接字符串
 dim cnstr
 cnstr = "driver={microsoft Access Driver (*.mdb)};dbq=" & server.MapPath("./UPLOAD.mdb")
%>


 
 单个图像保存到数据库
 
 
 

上传图片
 显示图片



 
<%
 if request.ServerVariables("REQUEST_METHOD") = "POST" then

 dim SCOme, sGo, binData, strData
 dim posB, posE, posSB, posSE
 dim binCrlf
 dim strPath, strFileName, strContentType
 
 binCrlf = chrb(13)&chrb(10) '定义一个单字节的回车换行符
 
 set sCome = server.Createobject("adodb.stream")
 sCome.Type = 1 '指定返回数据类型 adTypeBinary=1,adTypeText=2
 sCome.Mode = 3 '指定打开模式 adModeRead=1,adModeWrite=2,adModeReadWrite=3
 sCome.Open
 sCome.Write request.BinaryRead(request.TotalBytes)
 
 sCome.Position = 0
 binData = sCome.Read
 
 'response.BinaryWrite binData '调试用:显示提交的所有数据
 'response.Write "


" '调试用
 
 set sGo = server.CreateObject("adodb.stream")
 sGo.Type = 1
 sGo.Mode = 3
 sGo.Open
 
 posB = 1
 posB = instrb(posB,binData,binCrlf)
 posE = instrb(posB+1,binData,binCrlf)
 'response.Write posB & " | " & posE & "
"
 
 sCome.Position = posB+1
 sCome.CopyTo sGo,posE-posB-2
 sGo.Position = 0
 sGo.Type = 2
 sGo.Charset = "gb2312"
 strData = sGo.ReadText
 sGo.Close
 
 'response.Write strData & "
"
 
 posSB = 1
 posSB = instr(posSB,strData,"filename=""") + len("filename=""")
 posSE = instr(posSB,strData,"""")
 
 if posSE > posSB then
 strPath = mid(strData,posSB,posSE-posSB)
 'response.Write "本地路径:" & strPath & "
"
 'response.Write "文件名:" & getFileNamefromPath(strPath) & "
"
 
 posB = posE
 posE = instrb(posB+1,binData,binCrlf)
 'response.Write posB & " | " & posE & "
"
 
 sGo.Type = 1
 sGo.Mode = 3
 sGo.Open

 sCome.Position = posB
 sCome.CopyTo sGo,posE-posB-1

 sGo.Position = 0
 sGo.Type = 2
 sGo.Charset = "gb2312"
 strData = sGo.ReadText
 sGo.Close
 
 strContentType = mid(strData,16) '此处因为固定的,所以省略查找 :-)
 'response.Write "图片类型:" & strContentType & "


"
 
 posB = posE+2
 posE = instrb(posB+1,binData,binCrlf)
 'response.Write posB & " | " & posE & "
"
 
 sGo.Type = 1
 sGo.Mode = 3
 sGo.Open

 sCome.Position = posB+1
 sCome.CopyTo sGo,posE-posB-2
 
 sGo.Position = 0
 strData = sGo.Read
 sGo.Close
 
 'response.Write lenb(strData) & "
"
 
 dim cn, rs, sql
 set cn = server.CreateObject("adodb.connection")
 cn.Open cnstr
 set rs = server.CreateObject("adodb.recordset")
 sql = "select * from tblImage"
 rs.Open sql,cn,1,3
 rs.AddNew
 rs.Fields("content-type").Value = strContentType
 rs.Fields("image").AppendChunk strData
 rs.Update
 rs.Close
 set rs = nothing
 cn.Close
 set cn = nothing
 response.Write "图片保存成功!" & "
"
 else
 response.Write "没有上传图片!" & "
"
 end if
 
 set sGo = nothing
 sCome.Close
 set sCome = nothing 
 else
%>
 RM id="frmUpload" name="frmUpload" action="SimpleImageToData.asp" method="post" target="_self" enctype="multipart/form-data">
 
 

 
 
<%
 end if
%>
 

3、ShowImageListFromData.asp

<%@ Language=VBScript %>
<% option explicit %>



 显示数据库中已有图片的列表
 


上传图片
 显示图片




 
 
 
 

 <%
 dim cnstr
 cnstr = "driver={Microsoft Access Driver (*.mdb)};dbq=" & server.MapPath("./upload.mdb")
 
 dim cn, sql, rs
 set cn = server.CreateObject("adodb.connection")
 cn.Open cnstr
 sql = "select sn,[content-type],image from tblImage"
 set rs = cn.Execute(sql)
 
 response.Write ""
 response.Write ""
 response.Write ""
 response.Write ""
 
 do until rs.eof
 response.Write ""
 response.Write ""
 response.Write ""
 response.Write ""
 response.Write ""
 rs.movenext
 loop
 
 response.Write "
序列号图片类型图片
" & rs("sn") & "" & rs("content-type") & "看图
"
 
 cn.Close
 set cn = nothing
 %>
 

 <%
 dim sn
 sn = request.QueryString("sn")
 if sn = "" then
 response.Write "没有指定图片!"
 else
 response.Write ""
 end if
 %>
 


4、ShowImageFromData.asp

<%@ Language=VBScript %>
<% option explicit %>

<%
 dim sn
 sn = request.QueryString("sn")
 if sn = "" then response.End
 
 dim cnstr
 cnstr = "driver={Microsoft Access Driver (*.mdb)};dbq=" & server.MapPath("./upload.mdb")
 
 dim cn, sql, rs
 set cn = server.CreateObject("adodb.connection")
 cn.Open cnstr
 sql = "select sn,[content-type],image from tblImage where sn=" & cint(sn)
 set rs = cn.Execute(sql)
 
 response.ContentType = rs("content-type")
 response.BinaryWrite rs("image")
 
 set rs = nothing
 cn.Close
 set cn = nothing
%>

待续...



作者相关文章:
  • .NET/develop/article/19/19253.shtm" target=_blank>ASP文件上传神功 第二重(招势图加内功心法)(原作)
  • 网站的创意设计(收藏)
  • META标签的奥妙(收藏)
    其它相关文章:
  • 用JSPsmartupload进行上传下载(原作)
  • 在jsp中使用smartupload组件上传文件(原作)
  • 在jsp中使用smartupload组件上传文件(原作)
  • 在ASP.net中实现多文件上传(原作)
  • 也谈一下文件上传(原作)

    对该文的评论 人气:2536 2003-6-271715381.gif" width=16>   inanition(2003-6-26 23:10:17)
    在后面,先易后难嘛,如果一下子太深入,恐怕理解不了。

    我在看“化境HTTP上传程序”(1.0和2.0都仔细研究过)的时候,因为它已经集成封装成一个类了,用起来倒是简单,想了解原理就难了,我看了好几个晚上(我是不是很菜啊)。

    特别是处理中文的问题,多亏了稻香老农,再次感谢,有兴趣也可以下载一个看看,值得花几个晚上的。

     

    第二重:文本信息与图片文件同时提交保存到数据库
    图片文件也可保存到磁盘文件

      这个问题已经不是什么新鲜问题了,网上也有大把的教程,但大多数是授人以鱼,而不授人以渔,经过辛苦的资料收集,思考,调试,整理,我基本上已经把这个问题从原理上搞清楚了,现在根据我自己的理解,在范例程序的基础上,加以解释,希望能对部分网友(比我还菜的:-))有所帮助。

      请诸位大虾能对其中的不正或不良这处予以指正。

      程序中stream对象的用法上参考了“化境HTTP上传程序 Version 2.0”在代码,在此对稻香老农和梁无惧表示衷心的感谢和由衷的敬意。

      上次讲了单个图片文件保存到数据库,这次讲一下文本信息与图片文件同时提交保存到数据库,图片文件也可保存到磁盘文件。

    MultiInputOrImageToData.asp
    <%@ Language=VBScript %>
    <% option explicit %>

    <%
     '把一段二进制数据写入到一个文件
     sub saveBin2File(srmsource,posB,posLen,strPath)
      dim srmObj
      set srmObj = server.CreateObject("adodb.stream")
      srmObj.Type = 1
      srmObj.Mode = 3
      srmObj.Open
       
      srmSource.Position = posB-1
      srmSource.CopyTo srmObj,posLen
      srmObj.Position = 0
      srmObj.SaveToFile strPath,2   '如果该文件已经存在,无条件覆盖
      srmObj.Close
      set srmObj = nothing
     end sub

     '二进制数据转换为字符串,包括汉字
     function getTextfromBin(srmSource,posBegin,posLen)
      dim srmObj, strData
      set srmObj = server.CreateObject("adodb.stream")
      srmObj.Type = 1
      srmObj.Mode = 3
      srmObj.Open

      srmSource.position = posBegin-1   '位置计数首数不一样,这个对象是对0开始的
      srmSource.CopyTo srmObj,posLen
      srmObj.Position = 0
      srmObj.Type = 2
      srmObj.Charset = "gb2312"
      strData = srmObj.ReadText

      srmObj.Close
      set srmObj = nothing
       
      getTextfromBin = strData
     end function
        
     '双字节字符串转换成单字节字符串
     function getSBfromDB(bytString)
      dim bin, i
      bin = ""
      for  i=1 to len(bytString)
       bin = bin & chrb(asc(mid(bytString,i,1)))
      next
      getSBfromDB = bin
     end function

     '单字节字符串转换成双字节字符串
     function getDBfromSB(bitString)
      dim str, i
      str = ""
      for i=1 to lenb(bitString)
       str = str & chr(ascb(midb(bitString,i,1)))
      next
      getDBfromSB = str
     end function
     
     '从一个完整路径中析出文件名称
     function getFileNamefromPath(strPath)
      getFileNamefromPath = mid(strPath,instrrev(strPath,"")+1)
     end function

     '判断函数
     function iif(cond,expr1,expr2)
      if cond then
       iif = expr1
      else
       iif = expr2
      end if
     end function
     
     '定义数据库连接字符串
     dim cnstr
     cnstr = "driver={Microsoft Access Driver (*.mdb)};dbq=" & server.MapPath("./upload.mdb")
    %>


     
      多个表单域或图像同步保存到数据库
      
     
     

    导航菜单:上传图片 显示图片



     
    <%
     if request.ServerVariables("REQUEST_METHOD") = "POST" then

      dim sCome, binData
      dim posB, posE, posSB, posSE
      dim binCrlf, binSub
      dim strTitle, strFileName, strContentType, posfileBegin, posFileLen, aryFileInfo
      dim i, j
      dim dicData
      dim strName,strValue
      
      binCrlf = getSBfromDB(vbcrlf)  '定义一个单字节的回车换行符
      binSub = getSBfromDB("--")    '定义一个单字节的“--”字符串
      
      set sCome = server.CreateObject("adodb.stream")
      sCome.Type = 1  '指定返回数据类型 adTypeBinary=1,adTypeText=2
      sCome.Mode = 3  '指定打开模式 adModeRead=1,adModeWrite=2,adModeReadWrite=3
      sCome.Open
      sCome.Write request.BinaryRead(request.TotalBytes)
      
      sCome.Position = 0
      binData = sCome.Read
      'response.BinaryWrite binData    '调试用:显示提交的所有数据
      'response.Write "


    "       '调试用
      
      posB = instrb(binData,binSub)
      posB = instrb(posB,binData,bincrlf) + 2   '+2是加入回车换行符本身的长度
      posB = instrb(posB,binData,getSBfromDB("name=""")) + 6
      
      set dicData = server.CreateObject("scripting.dictionary")    '用来保存信息
      
      do until posB=6
       posE = instrb(posB,binData,getSBfromDB(""""))
       'Response.Write "name=" & getTextfromBin(sCome,posB,posE-posB) & "
    "
       strName = getTextfromBin(sCome,posB,posE-posB)
       
       posB = posE + 1     '指针移动到“"”的后面
       posE = instrb(posB,binData,bincrlf)
       'Response.Write posB & "->" & posE & "
    "
      
       if instrb(midb(binData,posB,posE-posB),getSBfromDB("filename=""")) > 0 then   '判断成功表示这是一个file域
        posB = instrb(posB,binData,getSBfromDB("filename=""")) + 10
        posE = instrb(posB,binData,getSBfromDB(""""))
        if posE>posB then
         'response.Write "filename=" & getTextfromBin(sCome,posB,posE-posB) & "
    "
         strFileName = getFileNamefromPath(getTextfromBin(sCome,posB,posE-posB))
         posB = instrb(posB,binData,getSBfromDb("Content-Type:")) + 14
         posE = instrb(posB,binData,bincrlf)
         'response.Write "content-type=" & getTextfromBin(sCome,posB,posE-posB) & "
    "
         strContentType = getTextfromBin(sCome,posB,posE-posB)

         posB = posE + 4     '这个地方换了两行,具体参看输出的原始二进制数据
         posE = instrb(posB,binData,binSub)
         'response.Write "image data="
        
         'saveBin2File sCome,posB,posE-posB-1,server.MapPath("./") & " est.jpg"
         'Response.Write "
    "
         posFileBegin = posB
         posFileLen = posE-posB-1
         strValue = strFileName & "," & strContentType & "," & posFileBegin & "," & posFileLen
        else
         'Response.Write "没有上传文件!" & "
    "
         strValue = ""
        end if
       else
        posB = posE + 4     '这个地方换了两行,具体参看输出的原始二进制数据
        posE = instrb(posB,binData,binCrlf)
        'Response.Write "value=" & getTextfromBin(sCome,posB,posE-posB) & "
    "  '这个后面没有“"”,所以不用-1
        strValue = getTextfromBin(sCome,posB,posE-posB)
       end if
      
       dicData.Add strName,strValue
       
       posB = posE + 2
      
       posB = instrb(posB,binData,bincrlf) + 2
       posB = instrb(posB,binData,getSBfromDB("name=""")) + 6
      loop
      
      strTitle = dicData.Item("txtTitle")
      aryFileInfo = dicData.Item("filImage")
      if aryFileInfo <> "" then         '有文件数据上传
       aryFileInfo = split(aryFileInfo,",")
       strFileName = aryFileInfo(0)
       strContentType = aryFileInfo(1)
       posFileBegin = aryFileInfo(2)
       posFileLen = aryFileInfo(3)
       
       sCome.Position = posFileBegin-1
       binData = sCome.Read(posFileLen)
            
       dim cn, rs, sql
       set cn = server.CreateObject("adodb.connection")
       cn.Open cnstr
       set rs = server.CreateObject("adodb.recordset")
       sql = "select title,[content-type],image from tblImage2"
       rs.Open sql,cn,1,3
       rs.AddNew
       rs.Fields("title").Value = strTitle
       rs.Fields("content-type").Value = strContentType
       
       '数据保存到数据库
       rs.Fields("image").AppendChunk binData

       '如果数据以文件形式保存到磁盘上,用下面这句
       'saveBin2File sCome,posFileBegin,posFileLen,server.MapPath("./") & strFileName

       rs.Update
       rs.Close
       set rs = nothing
       cn.Close
       set cn = nothing
       
       Response.Write "

    文件保存成功!

    "
      else
       Response.Write "

    没有上传文件!

    "
      end if
      
      sCome.Close
      set sCome = nothing  
     else
    %>
      
    " method="post" target="_self" enctype="multipart/form-data">
       

    标题:


       

    图片:


       
      

    <%
     end if
    %>
     

    待续...



    作者相关文章:
  • ASP文件上传神功 第一重(招势图加内功心法)(原作)
  • 网站的创意设计(收藏)
  • META标签的奥妙(收藏)
    其它相关文章:
  • 用jspsmartupload进行上传下载(原作)
  • 在jsp中使用smartupload组件上传文件(原作)
  • 在jsp中使用smartupload组件上传文件(原作)
  • 在ASP.NET中实现多文件上传(原作)
  • 也谈一下文件上传(原作)

    对该文的评论 人气:1114   inanition(2003-6-26 23:02:08)
    glenglenglen:
      对啊,改成这个更好,因为好辛苦才达到目的,代码可能不是很优化,谢谢指正。

    mrrdh007:
      你看看第一重就会明的了,stream对象的type=1表示返回的数据为二进制数据,=2表示是文本数据,mode=1表示可读,=2表示可写,=3表示可读写。

      mrrdh007(2003-6-26 18:46:48)
    srmObj.Type = 1
    srmObj.Mode = 3
    问一下,这是什么意思?不太懂
      glenglenglen(2003-6-26 17:11:27)
    getFileNamefromPath(strPath)函数中的mid是不是应该改为right?

     


  • 来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/10748419/viewspace-958125/ ,如需转载,请注明出处,否则将追究法律责任。

    上一篇: const的思考 (转)
    请登录后发表评论 登录
    全部评论
    • 博文量
      3121
    • 访问量
      2643809