这个上传文件有些地方看不懂哪位帮我解解惑

钱再多 2009-08-08 10:05:33
<%
'变量定义
Dim Data_Netout

'用户自定义的类
Class Netout
Dim objForm,objFile,Version

'定义的类的初始化函数
Private Sub Class_Initialize
dim RequestData,sStart,vbCrlf,sInfo,iInfoStart,iInfoEnd,tStream,iStart,theFile
dim iFileSize,sFilePath,sFileType,sFormValue,sFileName
dim iFindStart,iFindEnd
dim iFormStart,iFormEnd,sFormName

'创建dictionary对象
set objForm=Server.CreateObject("Scripting.Dictionary")
set objFile=Server.CreateObject("Scripting.Dictionary")

'获取提交的图片文件,如果为空则退出
if Request.TotalBytes<1 then Exit Sub

'创建ADODB的Stream流对象
'该对象用来读、写并管理一个二进制数据流或者文本数据流
set tStream = Server.CreateObject("adodb.stream")
set Data_Netout = Server.CreateObject("adodb.stream")

'类型值为1,设置为二进制数据流
Data_Netout.Type = 1

'设置为读写方式,3代表允许读/写
Data_Netout.Mode =3

'打开Stream对象
Data_Netout.Open
Data_Netout.Write Request.BinaryRead(Request.TotalBytes)

'设置数据流从开始到当前的位置
Data_Netout.Position=0

'读取全部的数据流
RequestData =Data_Netout.Read

iFormStart = 1
iFormEnd = LenB(RequestData)

'回车换行符的ASCII码值
vbCrlf = chrB(13) & chrB(10)
sStart = MidB(RequestData,1, InStrB(iFormStart,RequestData,vbCrlf)-1)
iStart = LenB (sStart)
iFormStart=iFormStart+iStart+1

while (iFormStart + 10) < iFormEnd
'流对象的一些初始化定义
iInfoEnd = InStrB(iFormStart,RequestData,vbCrlf & vbCrlf)+3
tStream.Type = 1
tStream.Mode =3
tStream.Open
Data_Netout.Position = iFormStart
Data_Netout.CopyTo tStream,iInfoEnd-iFormStart
tStream.Position = 0
tStream.Type = 2
tStream.Charset ="gb2312"
sInfo = tStream.ReadText
tStream.Close
'取得表单项目名称
iFormStart = InStrB(iInfoEnd,RequestData,sStart)
iFindStart = InStr(22,sInfo,"name=""",1)+6
iFindEnd = InStr(iFindStart,sInfo,"""",1)
sFormName = lcase(Mid (sinfo,iFindStart,iFindEnd-iFindStart))
'如果是文件
if InStr (45,sInfo,"filename=""",1) > 0 then
'取得文件信息
set theFile=new FileInfo
iFindStart = InStr(iFindEnd,sInfo,"filename=""",1)+10
iFindEnd = InStr(iFindStart,sInfo,"""",1)
sFileName = Mid (sinfo,iFindStart,iFindEnd-iFindStart)
'取得文件名
theFile.FileName=getFileName(sFileName)
'取得文件路径
theFile.FilePath=getFilePath(sFileName)
'取得文件类型
iFindStart = InStr(iFindEnd,sInfo,"Content-Type: ",1)+14
iFindEnd = InStr(iFindStart,sInfo,vbCr)
theFile.FileStart =iInfoEnd
'取得文件大小
theFile.FileSize = iFormStart -iInfoEnd -3
theFile.FormName=sFormName
if not objFile.Exists(sFormName) then
objFile.add sFormName,theFile
end if
else
'如果是表单项目
tStream.Type =1
tStream.Mode =3
tStream.Open
Data_Netout.Position = iInfoEnd
Data_Netout.CopyTo tStream,iFormStart-iInfoEnd-3
tStream.Position = 0
tStream.Type = 2
tStream.Charset ="gb2312"
sFormValue = tStream.ReadText
tStream.Close

'取得表单项目信息
if objForm.Exists(sFormName) then
objForm(sFormName)=objForm(sFormName)&", "&sFormValue
else
objForm.Add sFormName,sFormValue
end if
end if
iFormStart=iFormStart+iStart+1
wend
RequestData=""
set tStream =nothing
End Sub

'类结束的设置
Private Sub Class_Terminate
if Request.TotalBytes>0 then
objForm.RemoveAll
objFile.RemoveAll
set objForm=nothing
set objFile=nothing
Data_Netout.Close
set Data_Netout =nothing
end if
End Sub

'表单的设置方法
Public function Form(strForm)
strForm=lcase(strForm)
if not objForm.exists(strForm) then
Form=""
else
Form=objForm(strForm)
end if
end function

'创建文件信息对象方法
Public function File(strFile)
strFile=lcase(strFile)
if not objFile.exists(strFile) then
set File=new FileInfo
else
set File=objFile(strFile)
end if
end function

'获取文件路径的方法
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

'结束类定义
End Class

'文件信息类
Class FileInfo
dim FormName,FileName,FilePath,FileSize,FileStart

'类的初始化方法
Private Sub Class_Initialize
FileName = ""
FilePath = ""
FileSize = 0
FileStart= 0
FormName = ""
End Sub

'保存文件的方法
Public function SaveAs(FullPath)
dim dr,ErrorChar,i
SaveAs=true
if trim(fullpath)="" or FileStart=0 or FileName="" or right(fullpath,1)="/" then exit function
set dr=CreateObject("Adodb.Stream")
dr.Mode=3
dr.Type=1
dr.Open
Data_Netout.position=FileStart
Data_Netout.copyto dr,FileSize
dr.SaveToFile FullPath,2
dr.Close
set dr=nothing
SaveAs=false
end Function

'类定义结束
End Class
%>
<html>
<head>
<title>图片上传</title>
<link href="images/bbs.css" rel="stylesheet" type="text/css">
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<style type="text/css">
<!--
.style1 {color: #F2F8FF}
-->
</style>
</head>
<body>
<table width="365" border="0" cellspacing="0" cellpadding="0" align="center" bordercolordark="#CCCCCC" bordercolorlight="#000000">
<tr>
<td align="center">
<table width="75%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td height="48" align="center"><strong>图片上传信息</strong></td>
</tr>
</table>
<table width="50%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td align="center">
<%
dim UpImages,File,formName,formPath,iCount
dim gFilePath,gPath,gRootUrl
dim ImageInfo(6),Content(6),FilePath(6)

'建立上传对象
set UpImages=new Netout

'文件保存路径
formPath="pics/"

'统计上传图片文件个数
iCount=0
'统计上传图片文件大小
filesizecount=0

'列出所有上传了的文件
for each formName in UpImages.objFile
'生成一个文件对象

set File=UpImages.file(formName)

'如果 FileSize > 0 说明有文件数据
if File.FileSize>0 and instr("jpg,gif,bmp,png",Lcase(right(File.FileName,3)))>0 then
'生成随机数据
randomize
ranNum=int(900*rnd)+100

'文件名生成
FileName=formPath&year(now)&month(now)&day(now)&hour(now)&minute(now)&second(now)&ranNum&"."&right(File.FileName,3)
'保存文件
File.SaveAs Server.mappath(FileName)
iCount=iCount+1
filesizecount=filesizecount+File.FileSize
end if
set File=nothing
next

Msg=Msg&"总共上传<font color=red>"&iCount&"</font>张图片。"
Msg=Msg&"<br>总共大小:"&FormatNumber(filesizecount/1024,2)&"K。"

'删除此对象
set UpImages=nothing

ShowMsg Msg

sub ShowMsg(Msg)
set UpImages=nothing
response.write Msg
end sub
%>
</td>
</tr>
</table>

<table width="50%" height="40" border="0" cellpadding="0" cellspacing="0">
<tr>
<td align="center"><br><a href="index.asp">
<span style="text-decoration: none"><font size="2">查看图片</font></span></a><p><strong>还需要继续上传吗?</strong><br><Br><a href='javascript:history.back(-1);'>
<span style="text-decoration: none"><font size="2">确定</font></span></a><span class="7 style1">   </span><a href='javascript:window.close()' class='black'><span style="text-decoration: none"><font size="2">取消</font></span></a></td>
</tr>
</table>
</td>
</tr>
</table>
</body>
</html>

从BODY开始一直向下
244 行 for each formName in UpImages.objFile
formName是变量定义了 是使用for each 的开始位 是更具后面的结束位的类型 给定一个同类型的初始值 是这样吗

UpImages是自定义的类对象 后面的objFile 是什么意思 方法 还是属性 objFile在前面有一句定义set objFile=Server.CreateObject("Scripting.Dictionary")
...全文
54 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
hookee 2009-08-09
  • 打赏
  • 举报
回复
UpImages是类Netout的实例。
objFile是类的属性,VBS中没有声明Private的变量默认作为公开的属性。
objFile在类的初始化时被赋值为字典对象。
字典对象本身实现了枚举接口,定义有Count和Item属性 可用 for each语句遍历
for each中 forName的类型根据对象如何实现枚举接口来定,对于字典对象是根据key来返回item, forName得到的是key名字。

28,391

社区成员

发帖
与我相关
我的任务
社区描述
ASP即Active Server Pages,是Microsoft公司开发的服务器端脚本环境。
社区管理员
  • ASP
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧