ASP無組件上傳類代碼詳細說明

來源:http://hi.baidu.com/tianguipeng/blog/item/9a6832c6b936e4189c163d07.html

2007年09月22日 星期六 14:55
本文講述ASP無組件上傳類代碼詳細說明,此上傳類是在化境程式化界發布的無組件上傳類的基礎上修改的,在與化境程式化界無組件上傳類相比,速度快了將近50倍。
<iframe name="google_ads_frame" marginwidth="0" marginheight="0" src="http://pagead2.googlesyndication.com/pagead/ads?client=ca-pub-3795982983039684&dt=1190444013453&lmt=1190444013&format=300x250_as&output=html&correlator=1190444013390&channel=0503227947&url=http%3A%2F%2Fwww.mobanku.com%2Ffile%2Fasp%2F00533.asp&color_bg=FFFFFF&color_text=000000&color_link=0000FF&color_url=008000&color_border=FFFFFF&ad_type=text&ref=http%3A%2F%2Fcache.baidu.com%2Fc%3Fword%3Dasp%253B%25C0%25E0%253B%25B5%25C4%253B%25CF%25EA%25CF%25B8%253B%25CB%25B5%25C3%25F7%26url%3Dhttp%253A%2F%2Fwww%252Emobanku%252Ecom%2Ffile%2Fasp%2F00533%252Easp%26p%3D9e6fc64ad6c911a05ee7d73756649d%26user%3Dbaidu&cc=169&ga_vid=679029804.1190444013&ga_sid=1190444013&ga_hid=1596244964&flash=9&u_h=768&u_w=1280&u_ah=738&u_aw=1280&u_cd=32&u_tz=480&u_his=1&u_java=true" frameborder="0" width="300" scrolling="no" height="250" allowtransparency="allowtransparency"> 

'轉發時請保留此聲明資訊,這段聲明並不會影響你的速度!
'********* 無組件上傳類 *************
'最后修改者:塞北的雪
'blog:http://blog.csdn.net
'電子信件:northsnow@163.com

'聲明:此代碼是在梁無懼代碼基礎上修改的,沒有更改代碼內核,只是增加了一個屬性 smallFileName
'之所以發這篇文章,是想告訴大家,在使用高手一寫好的代碼的時候,不要僅局限於別人提供的現有的功能,
'而應該在他人提供的已有的功能的基礎上,根據自己的需求進行擴改。以達到自己最滿意的需求。

'修改者:梁無懼
'電子信件:yjlrb@21cn.com
'網站:http://www.25cn.com
'原作者:稻香老農
'原作者網站:http://www.5xsoft.com

'聲明:此上傳類是在化境程式化界發布的無組件上傳類的基礎上修改的.
'在與化境程式化界無組件上傳類相比,速度快了將近50倍,當上傳4M大小的文件時
'服務器只需要10秒就可以處理完,是目前最快的無組件上傳程序,當前版本為0.96
'源代碼公開,免費使用,對於商業用途,請與作者聯系
'文件屬性:例如上傳文件為c:\myfile\doc.txt
'FileName 文件名 字符串 "doc.txt"
'FileSize 文件大小 數值 1210
'FileType 文件類型 字符串 "text/plain"
'FileExt 文件擴展名 字符串 "txt"
'smallFileName 去掉了擴展名的文件名 "doc"
'FilePath 文件原路徑 字符串 "c:\myfile"
'使用時注意事項:
'由於Scripting.Dictionary區分大小寫,所以在網頁及ASP頁的項目名都要相同的大小
'寫,如果人習慣用大寫或小寫,為了防止出錯的話,可以把
'sFormName = Mid (sinfo,iFindStart,iFindEnd-iFindStart)
'改為
'(小寫者)sFormName = LCase(Mid (sinfo,iFindStart,iFindEnd-iFindStart))
'(大寫者)sFormName = UCase(Mid (sinfo,iFindStart,iFindEnd-iFindStart))
'*************************
'-------------------------
dim oUpFileStream

Class upload_file

dim Form,File

Private Sub Class_Initialize
'定義變量
dim RequestBinDate,sStart,bCrLf,sInfo,iInfoStart,iInfoEnd,
tStream,iStart,oFileInfo
dim iFileSize,sFilePath,sFileType,sFormvalue,sFileName
dim iFindStart,iFindEnd
dim iFormStart,iFormEnd,sFormName
'代碼開始
set Form = Server.CreateObject("Scripting.Dictionary")
set File = Server.CreateObject("Scripting.Dictionary")
if Request.TotalBytes < 1 then Exit Sub
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
RequestBinDate = oUpFileStream.Read
iFormEnd = oUpFileStream.Size
bCrLf = chrB(13) & chrB(10)
'取得每個項目之間的分隔符
sStart = MidB(RequestBinDate,1, InStrB(1,RequestBinDate,bCrLf)-1)
iStart = LenB (sStart)
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,sStart)-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
'取得文件屬性
iFindStart = InStr(iFindEnd,sInfo,"filename=""",1)+10
iFindEnd = InStr(iFindStart,sInfo,"""",1)
sFileName = Mid (sinfo,iFindStart,iFindEnd-iFindStart)
oFileInfo.FileName = GetFileName(sFileName)
oFileInfo.FilePath = GetFilePath(sFileName)
'oFileInfo.FileExt = GetFileExt(sFileName) '----劉金才修改
oFileInfo.FileExt = GetFileExt(oFileInfo.FileName) '----劉金才添加
oFileInfo.smallFileName = getSmallFileName(oFileInfo.FileName) '----劉金才添加
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
form.Add sFormName,sFormvalue
end if
tStream.Close
iFormStart = iFormStart+iStart+2
'如果到文件尾了就退出
loop until (iFormStart+2) = iFormEnd
RequestBinDate=""
set tStream = nothing
End Sub

Private Sub Class_Terminate
'清除變量及對像
if not Request.TotalBytes<1 then
oUpFileStream.Close
set oUpFileStream =nothing
end if
Form.RemoveAll
File.RemoveAll
set Form=nothing
set File=nothing
End Sub

'取得文件路徑
Private function GetFilePath(FullPath)
If FullPath <> "" Then
GetFilePath = left(FullPath,InStrRev(FullPath, "\"))
Else
GetFilePath = ""
End If
End function

'取得文件全名
Private function GetFileName(FullPath)
If FullPath <> "" Then
GetFileName = mid(FullPath,InStrRev(FullPath, "\")+1)
Else
GetFileName = ""
End If
End function

'取得擴展名
Private function GetFileExt(FileName)
If FileName <> "" Then
if instr(FileName,".")>0 then
GetFileExt = mid(FileName,InStrRev(FileName, ".")+1)
else
GetFileExt = ""
end if
Else
GetFileExt = ""
End If
End function

'取得去掉了擴展名的文件名 劉金才添加
Private function GetSmallFileName(FileName)
If FileName <> "" Then
if instr(FileName,".")>0 then
GetSmallFileName = mid(FileName,1,InStrRev(FileName, ".")-1)
else
GetSmallFileName = FileName
end if
Else
GetSmallFileName = ""
End If
End function

End Class

'文件屬性類
'新添加一個smallFileName 表示去掉了擴展名的文件名 劉金才添加
Class FileInfo
dim FormName,FileName,FilePath,FileSize,FileType,
FileStart,FileExt,smallFileName
Private Sub Class_Initialize
FileName = ""
smallFileName=""
FilePath = ""
FileSize = 0
FileStart= 0
FormName = ""
FileType = ""
FileExt = ""
End Sub

'保存文件方法
Public function SaveToFile(FullPath)
dim oFileStream,ErrorChar,i
SaveToFile=1
if trim(fullpath)="" or right(fullpath,1)="/" then exit function
set oFileStream=CreateObject("Adodb.Stream")
oFileStream.Type=1
oFileStream.Mode=3
oFileStream.Open
oUpFileStream.position=FileStart
oUpFileStream.copyto oFileStream,FileSize
oFileStream.SaveToFile FullPath,2
oFileStream.Close
set oFileStream=nothing
SaveToFile=0
end function
End Class
 

 

    全站熱搜

    sleepingwolf 發表在 痞客邦 留言(0) 人氣()