查看完整版本: asp无组件上传实例代码

cnnas 2007-11-4 00:26

asp无组件上传实例代码

[table=90%][tr][td][align=center][b]Asp[/b][b]无组件文件上传的实例[/b][/align]
[align=center][size=9pt][/size][/align]
[align=center][size=9pt][/size][/align]
[/td][/tr][tr][td][align=left][size=9pt]1.[/size][font=宋体][size=9pt]库文件[/size][/font][size=9pt](upload.inc.asp)
<%
Dim oUpFileStream [/size][/align]
[align=left][size=9pt]Class UpFile_Class[/size][/align]
[align=left][size=9pt]Dim Form,File,Version,Err [/size][/align]
[align=left][size=9pt]Private Sub Class_Initialize
Version = "[/size][font=宋体][size=9pt]无组件上传类[/size][/font][size=9pt] Version V1.0"
Err = -1
End Sub[/size][/align]
[align=left][size=9pt]Private Sub Class_Terminate
'[/size][font=宋体][size=9pt]清除变量及对像[/size][/font][size=9pt]
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)
'[/size][font=宋体][size=9pt]定义变量[/size][/font][size=9pt]
Dim RequestBinDate,sSpace,bCrLf,sInfo,iInfoStart,iInfoEnd,tStream,iStart,oFileInfo
Dim iFileSize,sFilePath,sFileType,sFormValue,sFileName
Dim iFindStart,iFindEnd
Dim iFormStart,iFormEnd,sFormName
'[/size][font=宋体][size=9pt]代码开始[/size][/font][size=9pt]
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
RequestBinDate = oUpFileStream.Read
iFormEnd = oUpFileStream.Size
bCrLf = ChrB (13) & ChrB (10)
'[/size][font=宋体][size=9pt]取得每个项目之间的分隔符[/size][/font][size=9pt]
sSpace = MidB (RequestBinDate,1, InStrB (1,RequestBinDate,bCrLf)-1)
iStart = LenB (sSpace)
iFormStart = iStart+2
'[/size][font=宋体][size=9pt]分解项目[/size][/font][size=9pt]
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
'[/size][font=宋体][size=9pt]取得表单项目名称[/size][/font][size=9pt]
iFormStart = InStrB (iInfoEnd,RequestBinDate,sSpace)-1
iFindStart = InStr (22,sInfo,"name=""",1)+6
iFindEnd = InStr (iFindStart,sInfo,"""",1)
sFormName = Mid (sinfo,iFindStart,iFindEnd-iFindStart)
'[/size][font=宋体][size=9pt]如果是文件[/size][/font][size=9pt]
If InStr (45,sInfo,"filename=""",1) > 0 Then
Set oFileInfo = new FileInfo_Class
'[/size][font=宋体][size=9pt]取得文件属性[/size][/font][size=9pt]
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
'[/size][font=宋体][size=9pt]如果是表单项目[/size][/font][size=9pt]
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
'[/size][font=宋体][size=9pt]如果到文件尾了就退出[/size][/font][size=9pt]
Loop Until (iFormStart+2) = iFormEnd
RequestBinDate = ""
Set tStream = Nothing
End Sub
End Class[/size][/align]
[align=left][size=9pt]'[/size][font=宋体][size=9pt]文件属性类[/size][/font][size=9pt]
Class FileInfo_Class
Dim FormName,FileName,FilePath,FileSize,FileType,FileStart,FileExt
'[/size][font=宋体][size=9pt]保存文件方法[/size][/font][size=9pt]
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

'[/size][font=宋体][size=9pt]取得文件数据[/size][/font][size=9pt]
Public Function FileDate
oUpFileStream.Position = FileStart
FileDate = oUpFileStream.Read (FileSize)
End Function
End Class
%>[/size][/align]
[align=left]
[b][size=9pt]2.[/size][/b][b][font=宋体][size=9pt]处理用户提交后的页面[/size][/font][/b][b][size=9pt](upload.asp)[/size][/b][size=9pt]
<!--#include file="upload.inc.asp"-->
<html>
<head>
<title>[/size][font=宋体][size=9pt]文件上传[/size][/font][size=9pt]</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()
'===========[/size][font=宋体][size=9pt]无组件上传[/size][/font][size=9pt](upload_0)====================
sub UpFile()
set upload=new UpFile_Class '[/size][font=宋体][size=9pt]建立上传对象[/size][/font][size=9pt]
upload.GetData (500*1024) '[/size][font=宋体][size=9pt]取得上传数据[/size][/font][size=9pt],[/size][font=宋体][size=9pt]此处即为[/size][/font][size=9pt]500 K

if upload.err > 0 then
select case upload.err
case 1
Response.Write "[/size][font=宋体][size=9pt]请先选择你要上传的文件 [/size][/font][size=9pt][ <a href=# onclick=history.go(-1)>[/size][font=宋体][size=9pt]重新上传[/size][/font][size=9pt]</a> ]"
case 2
Response.Write "[/size][font=宋体][size=9pt]图片大小超过了限制[/size][/font][size=9pt] 500 K[/size][font=宋体][size=9pt] [/size][/font][size=9pt][ <a href=# onclick=history.go(-1)>[/size][font=宋体][size=9pt]重新上传[/size][/font][size=9pt]</a> ]"
end select
exit sub
else
formPath=upload.form("filepath") '[/size][font=宋体][size=9pt]文件保存目录[/size][/font][size=9pt],[/size][font=宋体][size=9pt]此目录必须为程序可读写[/size][/font][size=9pt]
if formPath="" then
formPath="rwdata/"
end if
'[/size][font=宋体][size=9pt]在目录后加[/size][/font][size=9pt](/)
if right(formPath,1)<>"/" then
formPath=formPath&"/"
end if [/size][/align]
[align=left][size=9pt]for each formName in upload.file '[/size][font=宋体][size=9pt]列出所有上传了的文件[/size][/font][size=9pt]
set file=upload.file(formName) '[/size][font=宋体][size=9pt]生成一个文件对象[/size][/font][size=9pt]
if file.filesize<100 then
response.write "[/size][font=宋体][size=9pt]请先选择你要上传的图片 [/size][/font][size=9pt][ <a href=# onclick=history.go(-1)>[/size][font=宋体][size=9pt]重新上传[/size][/font][size=9pt]</a> ]"
response.end
end if[/size][/align]
[align=left][size=9pt]fileExt=lcase(file.FileExt)
if CheckFileExt(fileEXT)=false then
response.write "[/size][font=宋体][size=9pt]文件格式不正确 [/size][/font][size=9pt][ <a href=# onclick=history.go(-1)>[/size][font=宋体][size=9pt]重新上传[/size][/font][size=9pt]</a> ]"
response.end
end if[/size][/align]
[align=left][size=9pt]'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 '[/size][font=宋体][size=9pt]如果[/size][/font][size=9pt] FileSize > 0 [/size][font=宋体][size=9pt]说明有文件数据[/size][/font][size=9pt]
result=file.SaveToFile(Server.mappath(filename)) '[/size][font=宋体][size=9pt]保存文件[/size][/font][size=9pt]
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[/size][/align]
[align=left][size=9pt]'[/size][font=宋体][size=9pt]判断文件类型是否合格[/size][/font][size=9pt]
Private Function CheckFileExt (fileEXT)
dim Forumupload
Forumupload="gif,jpg,bmp,jpeg"
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>[/size][/align]
[align=left]
[b][size=9pt]3.HTML [/size][/b][b][font=宋体][size=9pt]表单[/size][/font][/b][b][size=9pt](upload.html)[/size][/b][size=9pt]
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="Content-Type" c>
<title>asp [/size][font=宋体][size=9pt]无组件上传[/size][/font][size=9pt]</title>
</head> [/size][/align]
[align=left][size=9pt]<body>
[/size][font=宋体][size=9pt]请选择要上传的文件[/size][/font][size=9pt]
<form action="upfile.asp" method="post" enctype="multipart/form-data" name="form1">
<input type="file" name="file">
<!--<br>
<input type="file" name="file">
<br>
<input type="file" name="file">
<br>-->
<input type="submit" name="Submit" value="[/size][font=宋体][size=9pt]提交[/size][/font][size=9pt]">
</form>
</body>
</html>[/size][/align]
[/td][/tr][/table][font=Times New Roman][/font]
页: [1]
查看完整版本: asp无组件上传实例代码