Today we'll talk about how to upload local files to the server using ASP's uploading capabilities.
The minimalist system consists of the following three files:
Upload.htm--Upload port file, select Local file
Uploadimg.asp--Uploading program control files
Upload_5xsoft.inc--No component upload class, this file beginners do not need to learn, as long as it can be used
Upload.htm content ———— Upload port file, select Local file
uploadimg.asp content ———— Upload program control files
<!--#include file= "upload_5xsoft.inc"-->
<%
Dim upload,file,filepath filepath=
"upload/" Set
upload=new upload_5xsoft ' Set up the upload object
for every formName in Upload.file ' lists all uploaded file
set File=upload.file ( FormName) ' Generates a file object
if filename. Filesize>0 then ' If FileSize > 0 indicates that there are file data
fname = File.filename files
. SaveAs Server.MapPath (filepath&fname) ' Save file End
if
set file=nothing
next
set upload= Nothing ' Delete this object
upload_5xsoft.inc Content ———— This file content does not belong to this walkthrough program content, this walkthrough applies the method of this class
<script runat=server language=vbscript> Dim oupfilestream Class Upload_5xsoft Dim form,file,version Private Su B 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= "No Component upload class 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 OUp
FileStream = Server.CreateObject ("ADODB.stream") Oupfilestream.type = 1 Oupfilestream.mode =3 oupfilestream.open Oupfilestream.write Request.BinaryRead (request.totalbytes) Response.Write "<font size=" "2" "> Page Execution Time:" & FormatNumber (Timer ()-time1) *1000,3) & "milliseconds </font><br>" oupfilestream.position=0 requestbindate = Oupfilestream.read Iformstart = 1 iformend = LenB (requestbindate) Bcrlf = ChrB (a) & CHRB (a) Sstart = MidB (requestbindate,1, InStrB (IFORMSTART,REQUESTBINDATE,BCRLF)-1) IStart = LenB (Sstart) iformstart= Iformstart+istart+1 while (Iformstart +) < Iformend Iinfoend = InStrB (Iformstart,requestbindate,bcrlf & BCrLf) +3 tstream.type = 1 Tstream.mode =3 tstream.open oupfilestream.position = Iformstart Oupfilestream.copyto tStream,iIn Foend-iformstart tstream.position = 0 Tstream.type = 2 Tstream.charset = "gb2312" sinfo = Tstream.readtext ' Get form item name Called Iformstart = InStrB (iinfoend,requestbindate,sstart) Ifindstart = InStr (22,sinfo, "name=" "", 1) +6 ifindend = InStr (ifind Start,sinfo, "" "", 1 sformname = Mid (sinfo,ifindstart,ifindend-ifindstart) ' If it is file if InStr (45,sinfo, "filename=" "", 1) & Gt 0 Then set ofileinfo=new FileInfo ' get filename Ifindstart = InStr (Ifindend,sinfo, "filename=" "", 1) +10 ifindend = InStr (ifinds
Tart,sinfo, "" "", 1 sFileName = Mid (Sinfo,ifindstart,ifindend-ifindstart) ofileinfo.filename=getfilename (sFileName) Ofileinfo.filepath=getfilepath(sFileName) ' Get file type Ifindstart = InStr (Ifindend,sinfo, "Content-type:", 1) +14 ifindend = InStr (IFINDSTART,SINFO,VBCR) Ofileinfo.fil EType =mid (sinfo,ifindstart,ifindend-ifindstart) ofileinfo.filestart =iinfoend ofileinfo.filesize = IFormStart- IInfoEnd-3 ofileinfo.formname=sformname file.add sformname,ofileinfo Else ' if it is a form item tstream.close tstream.type =1 t Stream.mode =3 Tstream.open oupfilestream.position = iinfoend Oupfilestream.copyto tStream,iFormStart-iInfoEnd-3 tStr Eam. 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 =nothi Ng 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 Priva
Te function GetFilePath (fullpath) If fullpath <> "" Then GetFilePath = Left (Fullpath,instrrev (FullPath, "")) Else GetFilePath = "" End-If End Function Private function G Etfilename (fullpath) If fullpath <> "" Then GetFileName = Mid (Fullpath,instrrev (FullPath, "") +1) Else Getfilen Ame = "" End If End Function End Class class FileInfo Dim Formname,filename,filepath,filesize,filetype,filestart Pri
Vate 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 (FULLP ath,1 = "/" Then Exit Function set Ofilestream=createobject ("ADODB.stream") ofilestream.type=1 ofilestream.mode=3 O Filestream.open oupfilestream.position=filestart oupfilestream.copyto ofilestream,filesize oFileStream.SaveToFile F ullpath,2 Ofilestream.close set ofilestream=nothing saveas=0 End Function End Class </SCRIPT>
This article is the content of the file to upload the most simplified program, please friends to analyze their own, learn this walkthrough, ASP General upload function to master the basic