网络编程
位置:首页>> 网络编程>> Asp编程>> 详解ASP图片上传保存到数据库功能(3)

详解ASP图片上传保存到数据库功能(3)

  发布时间:2008-01-25 18:47:00 

标签:数据库,图片,上传

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")
%>

<HTML>
 <HEAD>
  <title>多个表单域或图像同步保存到数据库</title>
  <meta http-equiv="Content-Type" content="text/html; charset=gb2312">
 </HEAD>
 <body>
<p>导航菜单:<b>上传图片</b> <a href="ShowImageListFromData2.asp">显示图片</a><hr></p>
 
<%
 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 "<hr>"       '调试用
  
  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) & "<br>"
   strName = getTextfromBin(sCome,posB,posE-posB)
   
   posB = posE + 1     '指针移动到“"”的后面
   posE = instrb(posB,binData,bincrlf)
   'Response.Write posB & "->" & posE & "<br>"
  
   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) & "<br>"
     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) & "<br>"
     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("./") & "\test.jpg"
     'Response.Write "<img src='test.jpg' border=0><br>"
     posFileBegin = posB
     posFileLen = posE-posB-1
     strValue = strFileName & "," & strContentType & "," & posFileBegin & "," & posFileLen
    else
     'Response.Write "没有上传文件!" & "<br>"
     strValue = ""
    end if
   else
    posB = posE + 4     '这个地方换了两行,具体参看输出的原始二进制数据
    posE = instrb(posB,binData,binCrlf)
    'Response.Write "value=" & getTextfromBin(sCome,posB,posE-posB) & "<br>"  '这个后面没有“"”,所以不用-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 "<p>文件保存成功!</p>"
  else
   Response.Write "<p>没有上传文件!</p>"
  end if
  
  sCome.Close
  set sCome = nothing  
 else
%>
  <form id="frmUpload" name="frmUpload" action="<%=Request.ServerVariables("script_name")%>" method="post" target="_self" enctype="multipart/form-data">
   <p>标题:<input id="txtTitle" type="text" name="txtTitle" size="40"></p>
   <p>图片:<INPUT id="filImage" type="file" name="filImage" size="40"></p>
   <INPUT id="btnUpload" type="submit" value="Upload" name="btnUpload">
  </form>
<%
 end if
%>
 </body>
</HTML>

0
投稿

猜你喜欢

  • JavaScript组件打包模式js组件通常带着css image ,但这样使用起来可能会有些小麻烦,为了让组件足够的solo,有了把css
  • 1.intersect为取多个查询结果的交集;2.查询两个基本时间段内表记录的SQL语句;select * from shengjibiao
  • 八种获取当前日期的js代码,第一步:把如下代码加入<head>区域中:<SCRIPT language=java
  • 在 MySQL下,在进行中文模糊检索时,经常会返回一些与之不相关的记录,如查找 "%a%" 时,返回的可能有中文字符,却
  • asp如何将RGB颜色转化成到16进制的?在R G B中输入小于255的数字点击观看即可转换成#开通的16进制。代码如下:<%R_RG
  • 很多人错误地认为ASP是简单、低效的代名词,认为ASP是低能的,不足挂齿的,也很简单,一学就会,一琢磨就精通。有人讲ASP不安全,写ASP的
  • 2.彻底弄懂CSS盒子模式二(导航栏实例) 3.彻底弄懂CSS盒子模式三(浮动的表演和清除的自述) 4.彻底弄懂CSS盒子模式四(绝对定位和
  • Request.ServerVariables("Url") 返回服务器地址Request.ServerVariable
  • 我一直都不喜欢在访问数据库时采用拼接SQL的方法,原因有以下几点: 1. 不安全:有被SQL注入的风险。 2. 可能会影响性能:每条SQL语
  • 设计首页的第一步是设计版面布局。就象传统的报刊杂志编辑一样,我们将网页看作一张报纸,一本杂志来进行排版布局。虽然 * 页技术的发展使得我们开
  • 网页上的图片如果设置了alt属性,当鼠标移经时就会有tooltip出现,但是只能显示一行文本,有时需要多行文本,乃至图片来显示图片、链接或者
  • 在做网站产品展示页面时,一般会用到缩略图,好处当然是直观醒目让人一目了然。点击进入然后看到大图及具体的介绍。但是缩略图在实现上带来了两个问题
  • 各大云计算提供商(亚马逊、谷歌和微软)目前都使用了键/值存储方式。然而,在San Francisco召开的MSDN开发者大会上,微软宣布他们
  • 有时候要用Javascript输常用的字符,比如每个页面都要有的脚注。这里提供一个转换脚本:将HTML自动转为JS代码<script&
  • 你一定很熟悉Youtube了,知道它是一个视频分享网站。是的,youtube目前十分流行,你也许会常常访问。这里有一些关于youtube u
  • 存储过程采用的是select top 加 not in的方式完成,速度也算是相当快了 我测试过了百万级数据量一般查询在1秒一下,贴出来大家交
  • 自从SQL Server 2005推出后,因为有了更好的性能,所以有很多与SQL Server 2000相关的应用程序需要升级到这个版本。但
  •            '把pattern 又修改了下'code
  • 从接触互联网开始,一直在关注每个网站页面浏览速度,不管大小国内外网站,而且每个用户的浏览时间都不一样,从而了解用户在浏览页面上时间体验尺度的
  • 内容摘要:在像网站首页这样的资源比较集中的页面中,那些栏目最经常被用户点击?居左居右对广告的点击率的影响是什么?“一切用数字说话”:以上问题
手机版 网络编程 asp之家 www.aspxhome.com