无组件实现文件上传/下载

'==================================================================
'
' 用ASP实现无组件上传/下载文件
'
' 功能简介
' 将上传的文件数据保存到数据库中,可以处理表单中的多个上传文件的情况
' 适用于各种数据库,使用ADO的方法连接数据库
' 本示例中使用的是ACCESS数据库:zj.mdb
' 表:tb_img(id int(自增列),path text(255) 保存上传文件的目录
' ,fname text(250) 保存上传的文件名,type test(250) 保存上传文件的类型
' ,img ole对象 保存上传的文件内容
'
'
'==================================================================

'==================================================================
'
' 上传文件的HTML页: zj_up.htm
'
'==================================================================

 1<html>
 2<head>
 3<title>文件上传保存到数据库中</title>
 4</head>
 5<body>
 6<form action="zj_up.asp" enctype="multipart/form-data" method="post" name="form1">
 7<p>
 8<input name="file" type="file"/>
 9<input name="Submit" type="submit" value="上传"/>
10</p>
11</form>
12</body>
13</html>

'==================================================================
'
' 上传文件保存到数据库的ASP页: zj_up.asp
'
'==================================================================

 1   
 2Response.Expires=0   
 3Function f_Bin2Str(ByVal sBin)   
 4Dim iI, iLen, iChr, iRe   
 5iRe = ""   
 6If Not IsNull(sBin) Then   
 7iLen = LenB(sBin)   
 8For iI = 1 To iLen   
 9iChr = MidB(sBin, iI, 1)   
10If AscB(iChr) > 127 Then   
11iRe = iRe & Chr(AscW(MidB(sBin, iI + 1, 1) & iChr))   
12iI = iI + 1   
13Else   
14iRe = iRe & Chr(AscB(iChr))   
15End If   
16Next   
17End If   
18f_Bin2Str = iRe   
19End Function   
20iConcStr = "Provider=Microsoft.Jet.OLEDB.4.0;Persist Security Info=False" & _   
21";Data Source=" & server.mappath("zj.mdb")   
22iSql="tb_img"   
23set iRe=Server.CreateObject("ADODB.Recordset")   
24iRe.Open iSql,iConcStr,1,3   
25iLen=Request.TotalBytes   
26sBin=Request.BinaryRead(iLen)   
27iCrlf1 = ChrB(13) & ChrB(10)   
28iCrlf2 = iCrlf1 & iCrlf1   
29iLen = InStrB(1, sBin, iCrlf1) - 1   
30iSpc = LeftB(sBin, iLen)   
31sBin = MidB(sBin, iLen + 34)   
32iPos1 = InStrB(sBin, iCrlf2) - 1   
33While iPos1 > 0   
34iStr = f_Bin2Str(LeftB(sBin, iPos1))   
35iPos1 = iPos1 + 5   
36iPos2 = InStrB(iPos1, sBin, iSpc)   
37  
38iPos3 = InStr(iStr, "; filename=""") + 12   
39If iPos3 > 12 Then   
40iStr = Mid(iStr, iPos3)   
41iPos3 = InStr(iStr, Chr(13) & Chr(10) & "Content-Type: ") - 2   
42iFn = Left(iStr, iPos3)   
43If iFn <> "" Then   
44iRe.AddNew   
45ire("path")=left(iFn,instrrev(iFn,"\"))   
46iRe("fname") = mid(iFn,instrrev(iFn,"\")+1)   
47iRe("type") = Mid(iStr, iPos3 + 18)   
48iRe("img").AppendChunk MidB(sBin, iPos1, iPos2 - iPos1)   
49iRe.Update   
50End If   
51End If   
52  
53sBin = MidB(sBin, iPos2 + iLen + 34)   
54iPos1 = InStrB(sBin, iCrlf2) - 1   
55Wend   
56iRe.close   
57set iRe=Nothing   

'==================================================================
'
' 下载数据的ASP页: zj_down.asp
'
'==================================================================

 1   
 2Response.Buffer=true   
 3Response.Clear 
 4
 5iConcStr = "Provider=Microsoft.Jet.OLEDB.4.0;Persist Security Info=False" & _   
 6";Data Source=" & server.mappath("zj.mdb")   
 7set iRe=server.createobject("adodb.recordset")   
 8iSql="tb_img"   
 9iRe.open iSql,iconcstr,1,1   
10Response.ContentType=ire("type")   
11Response.BinaryWrite iRe("img") 
12
13iRe.close   
14set iRe=Nothing   
Published At
Categories with Web编程
Tagged with
comments powered by Disqus