What should you do if you want to put the pictures or files of your machine on the server? There are three options: 1. FTP upload, 2. Use a USB flash drive to copy to the server, 3. If the server supports the ASP upload function, use the web page The browser uploads the file to the server.
Today we explain how to use the upload function of asp to upload local files to the server.
The simplest system includes the following three files:
upload.htm --Upload port file, select local file uploadimg.asp --Upload program control file upload_5xsoft.inc --No component upload class, beginners do not need to learn this file, as long as they can use it
upload.htm content--upload port file, select local file
- <html>
- <head>
- </head>
- <body>
- <tablewidth=80%border=0align=center>
- <formname=form1method=postaction=uploadimg.aspenctype=multipart/form-data>
- <tr>
- <tdalign=center><inputname=upfiletype=fileid=upfile></td>
- </tr>
- <tr>
- <tdalign=center><inputtype=submitname=Submitvalue=Upload pictures></td>
- </tr>
- </form>
- </table>
- </body>
- </html>
uploadimg.asp content--upload program control file
- <!--#includeFILE=upload_5xsoft.inc-->
- <%
- dimupload,file,filepath
- filepath=UPLOAD/
- setupload=newupload_5xSoft''Create upload object
- foreachformNameinupload.file'' lists all uploaded files
- setfile=upload.file(formName)'' generates a file object
- iffile.FileSize>0then''If FileSize>0, it means there is file data
- fname=file.filename
- file.SaveAsServer.mappath(filepath&fname)''Save file
- endif
- setfile=nothing
- next
- setupload=nothing''Delete this object
upload_5xsoft.inc content
--The content of this file does not belong to the content of this drill. This drill applies this method
- <SCRIPTRUNAT=SERVERLANGUAGE=VBSCRIPT>
- dimoUpFileStream
- Classupload_5xSoft
- dimForm,File,Version
- PrivateSubClass_Initialize
- dimRequestBinDate,sStart,bCrLf,sInfo,iInfoStart,iInfoEnd,tStream,iStart,oFileInfo
- dimiFileSize,sFilePath,sFileType,sFormvalue,sFileName
- dimiFindStart,iFindEnd
- dimiFormStart,iFormEnd,sFormName
- Version=No component upload class Version0.93
- setForm=Server.CreateObject(Scripting.Dictionary)
- setFile=Server.CreateObject(Scripting.Dictionary)
- ifRequest.TotalBytes<1thenExitSub
- settStream=Server.CreateObject(adodb.stream)
- setoUpFileStream=Server.CreateObject(adodb.stream)
- oUpFileStream.Type=1
- oUpFileStream.Mode=3
- oUpFileStream.Open
- oUpFileStream.WriteRequest.BinaryRead(Request.TotalBytes)
- Response.Write<fontsize=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(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.CopyTotStream,iInfoEnd-iFormStart
- tStream.Position=0
- tStream.Type=2
- tStream.Charset=gb2312
- sInfo=tStream.ReadText
- 'Get form item name
- iFormStart=InStrB(iInfoEnd,RequestBinDate,sStart)
- iFindStart=InStr(22,sInfo,name=,1)+6
- iFindEnd=InStr(iFindStart,sInfo,,1)
- sFormName=Mid(sinfo,iFindStart,iFindEnd-iFindStart)
- 'If it is a file
- ifInStr(45,sInfo,filename=,1)>0then
- setoFileInfo=newFileInfo
- 'Get the file name
- 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)
- 'Get file type
- 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.addsFormName,oFileInfo
- else
- 'If it is a form item
- tStream.Close
- tStream.Type=1
- tStream.Mode=3
- tStream.Open
- oUpFileStream.Position=iInfoEnd
- oUpFileStream.CopyTotStream,iFormStart-iInfoEnd-3
- tStream.Position=0
- tStream.Type=2
- tStream.Charset=gb2312
- sFormvalue=tStream.ReadText
- form.AddsFormName,sFormvalue
- endif
- tStream.Close
- iFormStart=iFormStart+iStart+1
- wend
- RequestBinDate=
- setStream=nothing
- EndSub
- PrivateSubClass_Terminate
- ifnotRequest.TotalBytes<1then
- form.RemoveAll
- file.RemoveAll
- setform=nothing
- setfile=nothing
- oUpFileStream.Close
- setoUpFileStream=nothing
- endif
- EndSub
- PrivatefunctionGetFilePath(FullPath)
- IfFullPath<>Then
- GetFilePath=left(FullPath,InStrRev(FullPath,))
- Else
- GetFilePath=
- EndIf
- End function
- PrivatefunctionGetFileName(FullPath)
- IfFullPath<>Then
- GetFileName=mid(FullPath,InStrRev(FullPath,)+1)
- Else
- GetFileName=
- EndIf
- End function
- EndClass
- ClassFileInfo
- dimFormName,FileName,FilePath,FileSize,FileType,FileStart
- PrivateSubClass_Initialize
- FileName=
- FilePath=
- FileSize=0
- FileStart=0
- FormName=
- FileType=
- EndSub
- PublicfunctionSaveAs(FullPath)
- dimoFileStream,ErrorChar,i
- SaveAs=1
- iftrim(fullpath)=orright(fullpath,1)=/thenexitfunction
- setoFileStream=CreateObject(Adodb.Stream)
- oFileStream.Type=1
- oFileStream.Mode=3
- oFileStream.Open
- oUpFileStream.position=FileStart
- oUpFileStream.copytooFileStream,FileSize
- oFileStream.SaveToFileFullPath,2
- oFileStream.Close
- setoFileStream=nothing
- SaveAs=0
- endfunction
- EndClass
- </SCRIPT>
What this article describes is the simplest program for uploading files. Friends, please analyze it yourself. After learning this drill, you will basically master the general upload function of ASP.