本页主题: 无组件实现文件上传/下载 打印 | 加为IE收藏 | 复制链接 | 收藏主题 | 上一主题 | 下一主题

不羁的风
级别: 精灵王


精华: 0
发帖: 210
威望: 146 点
金钱: 490 RMB
贡献值: 360 点
好评度: 58 点
在线时间:44(小时)
注册时间:2005-09-07
最后登录:2007-06-02

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

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

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


’==================================================================

’==================================================================

’ 上传文件的HTML页: zj_up.htm

’==================================================================
<html>
<head>
<title>文件上传保存到数据库中</title>
</head>
<body>
<form name="form1" 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_Bin2Str(ByVal sBin)
Dim iI, iLen, iChr, iRe
iRe = ""
If Not IsNull(sBin) Then
iLen = LenB(sBin)
For iI = 1 To iLen
iChr = MidB(sBin, iI, 1)
If AscB(iChr) > 127 Then
iRe = iRe & Chr(AscW(MidB(sBin, iI + 1, 1) & iChr))
iI = iI + 1
Else
iRe = iRe & Chr(AscB(iChr))
End If
Next
End If
f_Bin2Str = iRe
End Function
iConcStr = "Provider=Microsoft.Jet.OLEDB.4.0;Persist Security Info=False" & _
";Data Source=" & server.mappath("zj.mdb")
iSql="tb_img"
set iRe=Server.CreateObject("ADODB.Recordset")
iRe.Open iSql,iConcStr,1,3
iLen=Request.TotalBytes
sBin=Request.BinaryRead(iLen)
iCrlf1 = ChrB(13) & ChrB(10)
iCrlf2 = iCrlf1 & iCrlf1
iLen = InStrB(1, sBin, iCrlf1) - 1
iSpc = LeftB(sBin, iLen)
sBin = MidB(sBin, iLen + 34)
iPos1 = InStrB(sBin, iCrlf2) - 1
While iPos1 > 0
iStr = f_Bin2Str(LeftB(sBin, iPos1))
iPos1 = iPos1 + 5
iPos2 = InStrB(iPos1, sBin, iSpc)

iPos3 = InStr(iStr, "; filename=""") + 12
If iPos3 > 12 Then
iStr = Mid(iStr, iPos3)
iPos3 = InStr(iStr, Chr(13) & Chr(10) & "Content-Type: ") - 2
iFn = Left(iStr, iPos3)
If iFn <> "" Then
iRe.AddNew
ire("path")=left(iFn,instrrev(iFn,"\"))
iRe("fname") = mid(iFn,instrrev(iFn,"\")+1)
iRe("type") = Mid(iStr, iPos3 + 18)
iRe("img").AppendChunk MidB(sBin, iPos1, iPos2 - iPos1)
iRe.Update
End If
End If

sBin = MidB(sBin, iPos2 + iLen + 34)
iPos1 = InStrB(sBin, iCrlf2) - 1
Wend
iRe.close
set iRe=Nothing
%>
’==================================================================

’ 下载数据的ASP页: zj_down.asp

’==================================================================
<%
Response.Buffer=true
Response.Clear
iConcStr = "Provider=Microsoft.Jet.OLEDB.4.0;Persist Security Info=False" & _
";Data Source=" & server.mappath("zj.mdb")
set iRe=server.createobject("adodb.recordset")
iSql="tb_img"
iRe.open iSql,iconcstr,1,1
Response.ContentType=ire("type")
Response.BinaryWrite iRe("img")
iRe.close
set iRe=Nothing
%>


代码单一简单,适合学习使用,运用到网站中要防止此文件被利用而引起的安全问题。
<br>
启天网络
http://www.7tsky.com
顶端 Posted: 05-10-04 01:08 | [楼 主]
Mike
群熵工程师
级别: 管理员


精华: 7
发帖: 951
威望: 714 点
金钱: 6601 RMB
贡献值: 0 点
好评度: 611 点
在线时间:1405(小时)
注册时间:2003-11-28
最后登录:2018-04-23

 上传演示代码(看起来更轻松些)

<%
Response.Expires=0

Function f_Bin2Str(ByVal sBin)
     Dim iI, iLen, iChr, iRe
     iRe = ""
     If Not IsNull(sBin) Then
           iLen = LenB(sBin)
           For iI = 1 To iLen
                 iChr = MidB(sBin, iI, 1)
                 If AscB(iChr) > 127 Then
                       iRe = iRe & Chr(AscW(MidB(sBin, iI + 1, 1) & iChr))
                       iI = iI + 1
                 Else
                       iRe = iRe & Chr(AscB(iChr))
                 End If
           Next
     End If
     f_Bin2Str = iRe
End Function

iLen=Request.TotalBytes
sBin=Request.BinaryRead(iLen)
iCrlf1 = ChrB(13) & ChrB(10)
iCrlf2 = iCrlf1 & iCrlf1
iLen = InStrB(1, sBin, iCrlf1) - 1
iSpc = LeftB(sBin, iLen)
sBin = MidB(sBin, iLen + 34)
iPos1 = InStrB(sBin, iCrlf2) - 1
While iPos1 > 0
     iStr = f_Bin2Str(LeftB(sBin, iPos1))
     iPos1 = iPos1 + 5
     iPos2 = InStr(iStr, "name=""") + 6
     iPos3 = InStr(iStr, """; filename=""") + 13
     If iPos3 > 13 Then
           Response.Write "<br>Name:"&Mid(iStr, iPos2, iPos3-13-iPos2)
           iPos2 = InStrB(iPos1, sBin, iSpc)
           iStr = Mid(iStr, iPos3)
           iPos3 = InStr(iStr, Chr(13) & Chr(10) & "Content-Type: ") - 2
           iFn = Left(iStr, iPos3)
           If iFn <> "" Then
                 Response.Write "<br>Path: "&Left(iFn,InStrRev(iFn,"\"))
                 Response.Write "<br>FileName: "&Mid(iFn,InStrRev(iFn,"\")+1)
                 Response.Write "<br>Type: "&Mid(iStr, iPos3 + 18)
                 'Result: MidB(sBin, iPos1, iPos2 - iPos1)
           End If
     End If

     sBin = MidB(sBin, iPos2 + iLen + 34)
     iPos1 = InStrB(sBin, iCrlf2) - 1
Wend
%>
顶端 Posted: 05-10-15 17:35 | 1 楼
Mike
群熵工程师
级别: 管理员


精华: 7
发帖: 951
威望: 714 点
金钱: 6601 RMB
贡献值: 0 点
好评度: 611 点
在线时间:1405(小时)
注册时间:2003-11-28
最后登录:2018-04-23

 上传到文件的例子

以上是上传到数据库的例子,这里是上传到磁盘文件的例子,经过测试了:

upload.asp

Copy code
<!--#include file="upload.inc.asp"-->
<html>
<head>
<title>文件上传</title>
</head>
<body topmargin="0" leftmargin="0">
<table width=100% border=0 cellspacing="0" cellpadding="0"><tr><td class=tablebody1 width=100% height=100% >
<%
dim upload,file,formName,formPath,filename,fileExt
dim ranNum
call UpFile()
'===========无组件上传(upload_0)====================
sub UpFile()
set upload=new UpFile_Class '建立上传对象
upload.GetData (500*1024) '取得上传数据,此处即为500 K

if upload.err > 0 then
select case upload.err
case 1
Response.Write "请先选择你要上传的文件 [ <a href=# onclick=history.go(-1)>重新上传</a> ]"
case 2
Response.Write "图片大小超过了限制 500 K [ <a href=# onclick=history.go(-1)>重新上传</a> ]"
end select
exit sub
else
formPath=upload.form("filepath") '文件保存目录,此目录必须为程序可读写
if formPath="" then
formPath=""
end if
'在目录后加(/)
if right(formPath,1)<>"/" then
formPath=formPath&"/"
end if
for each formName in upload.file '列出所有上传了的文件
set file=upload.file(formName) '生成一个文件对象
if file.filesize<100 then
response.write "请先选择你要上传的图片 [ <a href=# onclick=history.go(-1)>重新上传</a> ]"
response.end
end if

fileExt=lcase(file.FileExt)
if CheckFileExt(fileEXT)=false then
response.write "文件格式不正确 [ <a href=# onclick=history.go(-1)>重新上传</a> ]"
response.end
end if

'randomize
ranNum=int(90000*rnd)+10000
filename=formPath&year(now)&month(now)&day(now)&hour(now)&minute(now)&second(now)&ranNum&"."&fileExt
if file.FileSize>0 then '如果 FileSize > 0 说明有文件数据
result=file.SaveToFile(Server.mappath(filename)) '保存文件
if result="ok" then
response.write formName&" upload OK, had saved to "&filename&"<br>"
else
response.write formName&" upload Fail,"&result&"<br>"
end if
end if
set file=nothing
next
set upload=nothing
end if
end sub

'判断文件类型是否合格
Private Function CheckFileExt (fileEXT)
dim Forumupload
Forumupload="gif,jpg,bmp,jpeg,doc"
Forumupload=split(Forumupload,",")
for i=0 to ubound(Forumupload)
if lcase(fileEXT)=lcase(trim(Forumupload(i))) then
CheckFileExt=true
exit Function
else
CheckFileExt=false
end if
next
End Function
%>
</td></tr></table>
</body>
</html>


upload.inc.asp

Copy code
<%
Dim oUpFileStream
Class UpFile_Class

Dim Form,File,Version,Err

Private Sub Class_Initialize
Version = "无组件上传类 Version V1.0"
Err = -1
End Sub

Private Sub Class_Terminate
'清除变量及对像
If Err < 0 Then
Form.RemoveAll
Set Form = Nothing
File.RemoveAll
Set File = Nothing
oUpFileStream.Close
Set oUpFileStream = Nothing
End If
End Sub

Public Sub GetData (RetSize)
'定义变量
Dim RequestBinDate,sSpace,bCrLf,sInfo,iInfoStart,iInfoEnd,tStream,iStart,oFileInfo
Dim iFileSize,sFilePath,sFileType,sFormValue,sFileName
Dim iFindStart,iFindEnd
Dim iFormStart,iFormEnd,sFormName
'代码开始
If Request.TotalBytes < 1 Then
Err = 1
Exit Sub
End If
If RetSize > 0 Then
If Request.TotalBytes > RetSize Then
Err = 2
Exit Sub
End If
End If
Set Form = Server.CreateObject ("Scripting.Dictionary")
Form.CompareMode = 1
Set File = Server.CreateObject ("Scripting.Dictionary")
File.CompareMode = 1
Set tStream = Server.CreateObject ("ADODB.Stream")
Set oUpFileStream = Server.CreateObject ("ADODB.Stream")
oUpFileStream.Type = 1
oUpFileStream.Mode = 3
oUpFileStream.Open
oUpFileStream.Write Request.BinaryRead (Request.TotalBytes)
'oUpFileStream.Position = 0

oUpFileStream.SaveToFile "D:\WWW\test2.txt",2
Response.Write oUpFileStream.Size
'Response.End

RequestBinDate = oUpFileStream.Read
iFormEnd = oUpFileStream.Size
bCrLf = ChrB (13) & ChrB (10)
'取得每个项目之间的分隔符
sSpace = MidB (RequestBinDate,1, InStrB (1,RequestBinDate,bCrLf)-1)
iStart = LenB (sSpace)
iFormStart = iStart+2
'分解项目
Do
iInfoEnd = InStrB (iFormStart,RequestBinDate,bCrLf & bCrLf)+3
tStream.Type = 1
tStream.Mode = 3
tStream.Open
oUpFileStream.Position = iFormStart
oUpFileStream.CopyTo tStream,iInfoEnd-iFormStart
tStream.Position = 0
tStream.Type = 2
tStream.CharSet = "gb2312"
sInfo = tStream.ReadText
'取得表单项目名称
iFormStart = InStrB (iInfoEnd,RequestBinDate,sSpace)-1
iFindStart = InStr (22,sInfo,"name=""",1)+6
iFindEnd = InStr (iFindStart,sInfo,"""",1)
sFormName = Mid (sinfo,iFindStart,iFindEnd-iFindStart)
'如果是文件
If InStr (45,sInfo,"filename=""",1) > 0 Then
Set oFileInfo = new FileInfo_Class
'取得文件属性
iFindStart = InStr (iFindEnd,sInfo,"filename=""",1)+10
iFindEnd = InStr (iFindStart,sInfo,"""",1)
sFileName = Mid (sinfo,iFindStart,iFindEnd-iFindStart)
oFileInfo.FileName = Mid (sFileName,InStrRev (sFileName, "\")+1)
oFileInfo.FilePath = Left (sFileName,InStrRev (sFileName, "\")+1)
oFileInfo.FileExt = Mid (sFileName,InStrRev (sFileName, ".")+1)
iFindStart = InStr (iFindEnd,sInfo,"Content-Type: ",1)+14
iFindEnd = InStr (iFindStart,sInfo,vbCr)
oFileInfo.FileType = Mid (sinfo,iFindStart,iFindEnd-iFindStart)
oFileInfo.FileStart = iInfoEnd
oFileInfo.FileSize = iFormStart -iInfoEnd -2
oFileInfo.FormName = sFormName
file.add sFormName,oFileInfo
else
'如果是表单项目
tStream.Close
tStream.Type = 1
tStream.Mode = 3
tStream.Open
oUpFileStream.Position = iInfoEnd
oUpFileStream.CopyTo tStream,iFormStart-iInfoEnd-2
tStream.Position = 0
tStream.Type = 2
tStream.CharSet = "gb2312"
sFormValue = tStream.ReadText
If Form.Exists (sFormName) Then
Form (sFormName) = Form (sFormName) & ", " & sFormValue
else
form.Add sFormName,sFormValue
End If
End If
tStream.Close
iFormStart = iFormStart+iStart+2
'如果到文件尾了就退出
Loop Until (iFormStart+2) = iFormEnd
RequestBinDate = ""
Set tStream = Nothing
End Sub
End Class

'文件属性类
Class FileInfo_Class
Dim FormName,FileName,FilePath,FileSize,FileType,FileStart,FileExt
'保存文件方法
Public Function SaveToFile (Path)
On Error Resume Next
Dim oFileStream
Set oFileStream = CreateObject ("ADODB.Stream")
oFileStream.Type = 1
oFileStream.Mode = 3
oFileStream.Open
oUpFileStream.Position = FileStart
oUpFileStream.CopyTo oFileStream,FileSize
oFileStream.SaveToFile Path,2
oFileStream.Close
Set oFileStream = Nothing
if Err.Number<>0 then
SaveToFile=err.number&"**"&Err.descripton
else
SaveToFile="ok"
end if
End Function

'取得文件数据
Public Function FileDate
oUpFileStream.Position = FileStart
FileDate = oUpFileStream.Read (FileSize)
End Function
End Class
%>



upload.html

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>asp 无组件上传</title>
</head>
<body>
请选择要上传的文件
<form action="upload.asp" method="post" enctype="multipart/form-data" name="form1">
<input type="file" name="file">
<input type="submit" name="Submit" value="提交">
</form>
</body>
</html>
顶端 Posted: 05-10-16 15:06 | 2 楼
帖子浏览记录 版块浏览记录
群熵信息 - 技术讨论区 » WEB脚本技术进阶

Total 0.031173(s) query 5, Time now is:06-18 08:13, Gzip enabled
Powered by PHPWind v6.3.2 Certificate Code © 2003-08 PHPWind.com Corporation

上海服务器租用 杭州世导双线服务器托管 云主机 VPS