详解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>


猜你喜欢
- 摘要:本文主要学习了如何使用DBUtils在Java代码中更方便的操作数据库。概述DBUtils是Java编程中的数据库操作实用工具,小巧简
- 一、需求介绍该需求主要是分析某一种数据的历史数据。客户的需求是根据该数据的前两期的情况,如果存在某个斜着的两个数字相等,那么就买第三期的同一
- 当我们的文章表中没有对于文章的评论数字段时,我们该这么写sql语句来显示出评论最多的文章呢?下面本站给大家收集了几种方法,仅供参考:1.se
- 由于系统自带的MySQL默认字符集不是gbk,因此给数据库的推广应用以及中文程序的开发带来极大的不便,在没完没了的GBK和UTF8的转换过程
- django 处理上传图片生成缩略图首先要注意form标签上必须有enctype="multipart/form-data&quo
- 在讲CSS优先级之前,我们得要了解什么是CSS,CSS是用来做什么的。首先,我们对CSS作一个简单的说明:CSS是层叠样式表(Cascadi
- 本文实例为大家分享了使用countdown插件实现倒计时的具体代码,供大家参考,具体内容如下实现的效果如下:这里实现的是一个活动倒计时,获取
- 最近对list设计感兴趣,今天说的是list视图方式的设计。感觉有些细节非常有意思,拿出来跟大家讨论。首先我们来看下windows下文件夹管
- title: 利用Django实现一个能与用户交互的初级框架author: Sun-Winddate: September 1, 2021D
- 太多的小伙伴正在学习Python,就说自己以后要做全栈开发,大家知道这是做什么的吗?我们现在所知道的知识点,哪些是以后你要从事这个全栈所需要
- 大部分面向对象的编程语言(除了C++)都只支持单继承,而不支持多继承,为什么呢?因为多继承不仅增加编程复杂度,而且容易导致莫名其妙的错误。P
- 众所周知,Jupyter notebook是一个交互式的Python shell,也就是IPython的封装版,非常适合用来进行数据分析和机
- JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式,采用完全独立于语言的文本格式,是理想的数据交换
- 一、概述 对象是Oracle8i以上版本中的一个新的特性,对象实际是对一组数据和操作的封装,对象的抽象就是类。在面向对象技术中,对象涉及到以
- 设想这样一个场景,你要给一个项目开发测试程序,程序开始运行的时候,会创建初始环境,测试完成以后,会清理环境。 这段逻辑本身非常简单
- Deferred对象结构Deferred由一系列成对的回调链组成,每一对都包含一个用于处理成功的回调(callbacks)和一个用于处理错误
- Web标准的web UI——来源、谬误与个人理解序我从2004年末开始接触web标准,2005年5月正式采取完全的web标准方式的网页制作,
- 反射在Python中,能够通过一个对象,找出type、class、attribute或者method的能力,成为反射。函数与方法内建函数:g
- 载入库绘制表格我们需要用到python库中的matplotlib库import matplotlib.pyplot as plt一、折线图#
- 1. 在猜年龄的基础上编写登录、注册方法,并且把猜年龄游戏分函数处理,如2. 登录函数3. 注册函数4. 猜年龄函数5. 选择奖品函数代码如