实例详解ASP的上传功能
请诸位大虾能对其中的不正或不良这处予以指正。
我想循序渐进,先讲一个简单的,单个图片文件保存到数据库。
这个范例共包括三个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")
%>
<HTML>
<HEAD>
<title>单个图像保存到数据库</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
</HEAD>
<body>
<p><a href="SimpleImageToData.asp">上传图片</a>
<a href="ShowImageListFromData.asp">显示图片</a><hr></p>
<%
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 "<hr>" ‘’调试用
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 & "<br>"
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 & "<hr>"
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 & "<br>"
‘’response.Write "文件名:" & getFileNamefromPath(strPath) & "<br>"
posB = posE
posE = instrb(posB+1,binData,binCrlf)
‘’response.Write posB & " | " & posE & "<br>"
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 & "<hr>"
posB = posE+2
posE = instrb(posB+1,binData,binCrlf)
‘’response.Write posB & " | " & posE & "<br>"
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) & "<br>"
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 "图片保存成功!" & "<br>"
else
response.Write "没有上传图片!" & "<br>"
end if
set sGo = nothing
sCome.Close
set sCome = nothing
else
%>
<form id="frmUpload" name="frmUpload" action="SimpleImageToData.asp" method="post" target="_self" enctype="multipart/form-data">
<INPUT id="filImage" type="file" name="filImage" size="40">
<BR>
<INPUT id="btnUpload" type="submit" value="Upload" name="btnUpload">
</form>
<%
end if
%>
</body>
</HTML>
[1] [2] [3] 下一页
- · ASP无组件上传类的应用实例
- · ASP程序编写完整的一个IP所在地搜索类
- · 用ASP读取网卡MAC地址
- · ASP程序中使用断开的数据记录集
- · 用ASP+XMLHTTP编写一个天气预报程序
- · 简单介绍ASP中Cache技术的应用
- · 如何使用ASP记录在线用户的数量
- · 在ASP应用程序中限制重复提交同一表单
- · 用ASP实现远程抓取网页到本地数据库
- · ASP程序中提供在线文章翻译功能
- · 在ASP应用程序中加入智能搜索
- · 20种看ASP程序源码的方法及工具
- · ASP程序实现在HTML中显示文章被阅读的次数
- · ASP对excel的操作
- · ASP有函数可以把某个网页通过STREAM下载
- · ASP设计动态页中服务器端的处理
- · ASP应用程序设计的Web状态管理分析
- · 如何轻松打造ASP计数器
- · 用ASP构建音乐服务器的方法
- · 在ASP中操作HTTP报头方法分析
- · 如何开始一个ASP网站的设计
- · 在ASP中访问和更新Cookies集合
- · 多个域名后缀同时查询的域名查询系统
- · 用ASP做资源管理器的树形目录
- · 动易2005版网站通用标签
- · 返回一个ASP页面执行后的静态HTML代码
- · Asp Object 之:PICS
- · Asp Object 之:Redirect
- · Asp Object 之:Flush
- · Asp Object 之:AddHeader
- · Asp Object 之:AppendToLog
- · Asp Object 之:BinaryWrite
- · Asp Object 之:Clear
- · Asp Object 之:ClientCertificate
- · Asp Object 之:Expires
- · Asp Object 之:IsClientConnected
- · Asp Object 之:Request
- · Asp Object 之:TotalBytes
