高手:
我自己做了一个同学录,请问如何实现无组件上传: 图片、REALPLAY文件(等视频文件)、MP3、WAV等
能给出一个完整代码吗?(一个程序实现多种格式的上传)
---------------------------------------------------------------
http://www.ccopus.com/code/upload.html
good luck.
---------------------------------------------------------------
稻乡老农
---------------------------------------------------------------
1<script language="VBSCRIPT" runat="SERVER">
2dim Data_5xsoft
3
4Class upload_5xsoft
5
6dim objForm,objFile,Version
7
8Public function Form(strForm)
9strForm=lcase(strForm)
10if not objForm.exists(strForm) then
11Form=""
12else
13Form=objForm(strForm)
14end if
15end function
16
17Public function File(strFile)
18strFile=lcase(strFile)
19if not objFile.exists(strFile) then
20set File=new FileInfo
21else
22set File=objFile(strFile)
23end if
24end function
25
26
27Private Sub Class_Initialize
28dim RequestData,sStart,vbCrlf,sInfo,iInfoStart,iInfoEnd,tStream,iStart,theFile
29dim iFileSize,sFilePath,sFileType,sFormValue,sFileName
30dim iFindStart,iFindEnd
31dim iFormStart,iFormEnd,sFormName
32Version="Version 2.0"
33set objForm=Server.CreateObject("Scripting.Dictionary")
34set objFile=Server.CreateObject("Scripting.Dictionary")
35if Request.TotalBytes<1 then Exit Sub
36set tStream = Server.CreateObject("adodb.stream")
37set Data_5xsoft = Server.CreateObject("adodb.stream")
38Data_5xsoft.Type = 1
39Data_5xsoft.Mode =3
40Data_5xsoft.Open
41Data_5xsoft.Write Request.BinaryRead(Request.TotalBytes)
42Data_5xsoft.Position=0
43RequestData =Data_5xsoft.Read
44
45iFormStart = 1
46iFormEnd = LenB(RequestData)
47vbCrlf = chrB(13) & chrB(10)
48sStart = MidB(RequestData,1, InStrB(iFormStart,RequestData,vbCrlf)-1)
49iStart = LenB (sStart)
50iFormStart=iFormStart+iStart+1
51while (iFormStart + 10) < iFormEnd
52iInfoEnd = InStrB(iFormStart,RequestData,vbCrlf & vbCrlf)+3
53tStream.Type = 1
54tStream.Mode =3
55tStream.Open
56Data_5xsoft.Position = iFormStart
57Data_5xsoft.CopyTo tStream,iInfoEnd-iFormStart
58tStream.Position = 0
59tStream.Type = 2
60tStream.Charset ="gb2312"
61sInfo = tStream.ReadText
62tStream.Close
63'取得表单项目名称
64iFormStart = InStrB(iInfoEnd,RequestData,sStart)
65iFindStart = InStr(22,sInfo,"name=""",1)+6
66iFindEnd = InStr(iFindStart,sInfo,"""",1)
67sFormName = lcase(Mid (sinfo,iFindStart,iFindEnd-iFindStart))
68'如果是文件
69if InStr (45,sInfo,"filename=""",1) > 0 then
70set theFile=new FileInfo
71'取得文件名
72iFindStart = InStr(iFindEnd,sInfo,"filename=""",1)+10
73iFindEnd = InStr(iFindStart,sInfo,"""",1)
74sFileName = Mid (sinfo,iFindStart,iFindEnd-iFindStart)
75theFile.FileName=getFileName(sFileName)
76theFile.FilePath=getFilePath(sFileName)
77'取得文件类型
78iFindStart = InStr(iFindEnd,sInfo,"Content-Type: ",1)+14
79iFindEnd = InStr(iFindStart,sInfo,vbCr)
80theFile.FileType =Mid (sinfo,iFindStart,iFindEnd-iFindStart)
81theFile.FileStart =iInfoEnd
82theFile.FileSize = iFormStart -iInfoEnd -3
83theFile.FormName=sFormName
84if not objFile.Exists(sFormName) then
85objFile.add sFormName,theFile
86end if
87else
88'如果是表单项目
89tStream.Type =1
90tStream.Mode =3
91tStream.Open
92Data_5xsoft.Position = iInfoEnd
93Data_5xsoft.CopyTo tStream,iFormStart-iInfoEnd-3
94tStream.Position = 0
95tStream.Type = 2
96tStream.Charset ="gb2312"
97sFormValue = tStream.ReadText
98tStream.Close
99if objForm.Exists(sFormName) then
100objForm(sFormName)=objForm(sFormName)&", "&sFormValue
101else
102objForm.Add sFormName,sFormValue
103end if
104end if
105iFormStart=iFormStart+iStart+1
106wend
107RequestData=""
108set tStream =nothing
109End Sub
110
111Private Sub Class_Terminate
112if Request.TotalBytes>0 then
113objForm.RemoveAll
114objFile.RemoveAll
115set objForm=nothing
116set objFile=nothing
117Data_5xsoft.Close
118set Data_5xsoft =nothing
119end if
120End Sub
121
122
123Private function GetFilePath(FullPath)
124If FullPath <> "" Then
125GetFilePath = left(FullPath,InStrRev(FullPath, "\"))
126Else
127GetFilePath = ""
128End If
129End function
130
131Private function GetFileName(FullPath)
132If FullPath <> "" Then
133GetFileName = mid(FullPath,InStrRev(FullPath, "\")+1)
134Else
135GetFileName = ""
136End If
137End function
138End Class
139
140Class FileInfo
141dim FormName,FileName,FilePath,FileSize,FileType,FileStart
142Private Sub Class_Initialize
143FileName = ""
144FilePath = ""
145FileSize = 0
146FileStart= 0
147FormName = ""
148FileType = ""
149End Sub
150
151Public function SaveAs(FullPath)
152dim dr,ErrorChar,i
153SaveAs=true
154if trim(fullpath)="" or FileStart=0 or FileName="" or right(fullpath,1)="/" then exit function
155set dr=CreateObject("Adodb.Stream")
156dr.Mode=3
157dr.Type=1
158dr.Open
159Data_5xsoft.position=FileStart
160Data_5xsoft.copyto dr,FileSize
161dr.SaveToFile FullPath,2
162dr.Close
163set dr=nothing
164SaveAs=false
165end function
166End Class
167</script>
---------------------------------------------------------------
化境ASP无组件上传类 - upload_5xsoft 使用手册 2.0
http://www.5xsoft.com/ [ 下载 ]
特别感谢“梁无惧”提供高效处理数据的方法
目 录
1.What's New?
2.关于 upload_5xsoft
3.运行平台与注意事项
4.如何得到表单数据
5.如何得到文件数据
6.类的成员与对象
7.使用示例
what's new
1.使用了网友“梁无惧” 提供的高效的处理方式,上传速度可提高一倍以上,可上传更大的文件。
2.添加了form 方法和file方法,把原来的 form 集和 file改为 objForm 和 objFile,避免了若上传时没有数据会出错的Bug。
3.对多选框的优化,上传同名的多选框时,会自动用 ", " 连接起来。
4.对于表单名,不再区分大小写,使用ASP使用者更习惯。
关于 upload_5xsoft
一直以来,由于FileSystemObject的局限,所以ASP最大的难题就是文件上传,大多解决法就是安装
第三方上传组件。可第三方组件有很多问题,有的组件要注册,有的组件要在表单中加上他的版权信息。
还有的就是组件的兼容问题。
在网上也流传了很多无组件上传的代码,但都是只能上传文本文件,或是只能将文件上传到数据库中。
我这段时间在研究ASP,发现可以不用第三方组件上传任意类型的文件。就写了这个类,给大家一
个方便,整个类放在一个文件中: upload_5xsoft.inc 在 Example 目录下还有一个完整的多文件上传示
例程序,可以直接使用。
申明:源代码是完全开放的,可能随意传播,但请保留其完整性,未经作者同意,不得用于商业。
运行平台与注意事项
a)可直接运行于 Windows2000+IIS 5
NT4 或是 Win98+PWS, 要安装ADO2.6 下载地址:http://www.microsoft.com/data/:
就行了
b) 在使用文件上传时, 表单 form 要加上 enctype="multipart/form-data" 即:
1<form action="" enctype="multipart/form-data" method="post" name="form1">
2<input name="text1" type="text" value="abc"/>
3<input name="file" type="file"/>
4<input name="submit" type="submit" value="提交"/>
5</form>
upload_5xsoft的对象
如定义一个上传对象
1
2set upload=new upload_5xsoft 'upload就是一个对象
upload_5xsoft 对象成员
File 方法,得到文件对象,例如:set file=upload.file("file1")
文件对象成员下面有说明
objFile 文件对象集,(是个dictionary对象)
文件对象成员:
Count 属性,文件表单的个数
FileName 属性,上传文件的名字
FileSize 属性,上传文件的大小(为0是表示没有文件)
FilePath 属性,上传前文件所在的路径
FormName 属性,文件表单的名字
SaveAs 方法,储存上传后文件,有一个参数,路径要为真实路径如:
例子: set file=upload.file("file1") 'file1为表单名
response.write "
1<br/>
文件名:"&file.FileName
response.write "
1<br/>
文件大小:"&file.FileSize
response.write "
1<br/>
文件路径:"&file.FilePath
file.saveAs Server.mappath("/1.jpg")
set file=nothing
Form 方法,获得表单数据,如 Response.Write upload.Form("abc")
objForm 表单数据集,(是个dictionary对象)用来代替 Request.Form
count 属性,表单数
exists 方法,检查是否有指定的表单名
更多的用法可看 vbscript 的dictionary对象帮助
例子:
'得到text1表单的数据,uplaod就是一开始创建的对象
sText=upload.form("text1")
Version 属性,upload_5xsoft类的版本号,如: