菜鸟有难了,怎么用asp实现文件上传?

菜鸟有难了,怎么用asp实现文件上传?最好有原代码的
---------------------------------------------------------------

一、这是上传到文件夹
1.把下面的代码保存为upload_5xsoft.inc

  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="化境HTTP上传程序 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>

2.用户界面uplfile.htm

1<html><title>example</title>
2<body>
3<!--#include FILE="upload_5xsoft.inc"-->   

'strFiel=request.form("file1")
'response.write strFiel
set upload=new upload_5xsoft
set file=upload.file("file1")
response.write upload.form("submit")&amp;"<br/>"
if file.fileSize&gt;0 then
file.saveAs Server.mappath("index.htm")
response.write "<br/>上传文件:"&amp;file.FileName
response.write "<br/>文件大小:"&amp;file.FileSize
end if
set file=nothing
set upload=nothing

1</html>

---------------------------------------------------------------

http://www.aspcool.com/download/display.asp?Did=287

用这个上传组件,挺好用的!
---------------------------------------------------------------

这个站点有个组件上传的例子,现成的下载
http://www.5xsoft.com
---------------------------------------------------------------

http://xingworld.net/help/xingerup/

有详细说明和代码

Published At
Categories with Web编程
comments powered by Disqus