’==================================================================
’
’ 用ASP实现无组件上传/下载文件
’
’ 功能简介
’ 将上传的文件数据保存到数据库中,可以处理表单中的多个上传文件的情况
’ 适用于各种数据库,使用ADO的方法连接数据库
’ 本示例中使用的是ACCESS数据库:zj.mdb
’ 表:tb_img(id int(自增列),path text() 保存上传文件的目录
’ ,fname text(0) 保存上传的文件名,type test(0) 保存上传文件的类型
’ ,img ole对象 保存上传的文件内容
’
’
’==================================================================
’==================================================================
’
’ 上传文件的HTML: zj_up.htm
’
’==================================================================
<html>
<head>
<title>文件上传保存到数据库中</title>
</head>
<body>
<form name="form" enctype="multipart/form-data" method="post" action="zj_up.asp">
<p>
<input type="file" name="file">
<input type="submit" name="Submit" value="上传">
</p>
</form>
</body>
</html>
’==================================================================
’
’ 上传文件保存到数据库的ASP: zj_up.asp
’
’==================================================================
<%
Response.Expires=0
Function f_BinStr(ByVal sBin)
Dim iI, iLen, iChr, iRe
iRe = ""
If Not IsNull(sBin) Then
iLen = LenB(sBin)
For iI = To iLen
iChr = MidB(sBin, iI, )
If AscB(iChr) > 7 Then
iRe = iRe & Chr(AscW(MidB(sBin, iI + , ) & iChr))
iI = iI +
Else
iRe = iRe & Chr(AscB(iChr))
End If
Next
End If
f_BinStr = iRe
End Function
iConcStr = "Provider=Microsoft.Jet.OLEDB..0;Persist Security Info=False" & _
";Data Source=" & server.mappath("zj.mdb")
iSql="tb_img"
set iRe=Server.CreateObject("ADODB.Recordset")
iRe.Open iSql,iConcStr,,
iLen=Request.TotalBytes
sBin=Request.BinaryRead(iLen)
iCrlf = ChrB() & ChrB(0)
iCrlf = iCrlf & iCrlf
iLen = InStrB(, sBin, iCrlf) -
iSpc = LeftB(sBin, iLen)
sBin = MidB(sBin, iLen + )
iPos = InStrB(sBin, iCrlf) -
While iPos > 0
iStr = f_BinStr(LeftB(sBin, iPos))
iPos = iPos +
iPos = InStrB(iPos, sBin, iSpc)
iPos = InStr(iStr, "; filename=""") +
If iPos > Then
iStr = Mid(iStr, iPos)
iPos = InStr(iStr, Chr() & Chr(0) & "Content-Type: ") -
iFn = Left(iStr, iPos)
If iFn <> "" Then
iRe.AddNew
ire("path")=left(iFn,instrrev(iFn,"\"))
iRe("fname") = mid(iFn,instrrev(iFn,"\")+)
iRe("type") = Mid(iStr, iPos + 8)
iRe("img").AppendChunk MidB(sBin, iPos, iPos - iPos)
iRe.Update
End If
End If
sBin = MidB(sBin, iPos + iLen + )
iPos = InStrB(sBin, iCrlf) -
Wend
iRe.close
set iRe=Nothing
%>
’==================================================================
’
’ 下载数据的ASP: zj_down.asp
’
’==================================================================
<%
Response.Buffer=true
Response.Clear
iConcStr = "Provider=Microsoft.Jet.OLEDB..0;Persist Security Info=False" & _
";Data Source=" & server.mappath("zj.mdb")
set iRe=server.createobject("adodb.recordset")
iSql="tb_img"
iRe.open iSql,iconcstr,,
Response.ContentType=ire("type")
Response.BinaryWrite iRe("img")
iRe.close
set iRe=Nothing
%>









