今天我们讲解如何利用asp的上传功能将本地的文件上传到服务器上。
最简系统包括下面三个文件:
upload.htm --上传口文件,选择本地文件
uploadimg.asp --上传程序控制文件
upload_5xsoft.inc --无组件上传类,此文件初学者不用学习,只要会用就可以了
upload.htm内容————上传口文件,选择本地文件
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
< html >
< head >
</ head >
< body >
< table width = "80%" border = "0" align = "center" >
< form name = "form1" method = "post" action = "uploadimg.asp" enctype = "multipart/form-data" >
< tr >
< td align = "center" >< input name = "upfile" type = "file" id = "upfile" ></ td >
</ tr >
< tr >
</ tr >
</ form >
</ table >
</ body >
</ html >
|
uploadimg.asp内容————上传程序控制文件
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
<!--#include FILE="upload_5xsoft.inc"-->
<%
dim upload,file,filepath
filepath="UPLOAD/"
set upload=new upload_5xSoft ''建立上传对象
for each formName in upload.file ''列出所有上传了的文件
set file=upload.file(formName) ''生成一个文件对象
if file.FileSize>0 then ''如果 FileSize > 0 说明有文件数据
fname = file.filename
file.SaveAs Server.mappath(filepath&fname) ''保存文件
end if
set file=nothing
next
set upload=nothing ''删除此对象
|
upload_5xsoft.inc内容————此文件内容不属于本演练程序内容,本演练应用此类的方法
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
|
<SCRIPT RUNAT=SERVER LANGUAGE=VBSCRIPT>
dim oUpFileStream
Class upload_5xSoft
dim Form,File,Version
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
Version= "无组件上传类 Version 0.93"
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)
Response.Write "<font size=" "2" ">页面执行时间:" &FormatNumber((Timer() -time1)*1000,3)& "毫秒</font><br>"
oUpFileStream.Position=0
RequestBinDate =oUpFileStream.Read
iFormStart = 1
iFormEnd = LenB(RequestBinDate)
bCrLf = chrB(13) & chrB(10)
sStart = MidB(RequestBinDate,1, InStrB(iFormStart,RequestBinDate,bCrLf)-1)
iStart = LenB (sStart)
iFormStart=iFormStart+iStart+1
while (iFormStart + 10) < iFormEnd
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)
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)
' 取得文件类型
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 -3
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-3
tStream.Position = 0
tStream.Type = 2
tStream.Charset = "gb2312"
sFormvalue = tStream.ReadText
form.Add sFormName,sFormvalue
end if
tStream.Close
iFormStart=iFormStart+iStart+1
wend
RequestBinDate= ""
set tStream =nothing
End Sub
Private Sub Class_Terminate
if not Request.TotalBytes<1 then
form.RemoveAll
file.RemoveAll
set form=nothing
set file=nothing
oUpFileStream.Close
set oUpFileStream =nothing
end if
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
End Class
Class FileInfo
dim FormName,FileName,FilePath,FileSize,FileType,FileStart
Private Sub Class_Initialize
FileName = ""
FilePath = ""
FileSize = 0
FileStart= 0
FormName = ""
FileType = ""
End Sub
Public function SaveAs(FullPath)
dim oFileStream,ErrorChar,i
SaveAs=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
SaveAs=0
end function
End Class
</SCRIPT>
|
此文所诉的内容是上传文件的最简化程式,请朋友们自己分析一下,学会本演练,asp一般的上传功能就基本掌握了