asp Upload local files to server

  • 2021-07-02 23:52:35
  • OfStack

Today we show you how to use asp's upload capability to upload local files to the server.
The minimalist system consists of 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, this file beginners do not need to learn, as long as you can use it

upload. htm Content-Upload port file, select local file


<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>
  <td align="center"><input type="submit" name="Submit" value=" Upload pictures "></td>
 </tr>
 </form>
</table>
</body>
</html>

uploadimg. asp Content-Upload Program Control File


<!--#include FILE="upload_5xsoft.inc"-->
<%
dim upload,file,filepath
filepath="UPLOAD/"
set upload=new upload_5xSoft '' Create Upload Object 
for each formName in upload.file '' List all uploaded files 
 set file=upload.file(formName) '' Generate 1 File objects 
 if file.FileSize>0 then     '' If  FileSize > 0  Explain that there is file data 
 fname = file.filename
 file.SaveAs Server.mappath(filepath&fname)  '' Save a file 
 end if
set file=nothing
next
set upload=nothing '' Delete this object 

upload_5xsoft. inc content--This file content is not part of this walkthrough, this walkthrough applies this type of method


<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=" Upload class without component  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""> 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.CopyTo tStream,iInfoEnd-iFormStart
 tStream.Position = 0
 tStream.Type = 2
 tStream.Charset ="gb2312"
 sInfo = tStream.ReadText   
 ' Get the 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 
 if InStr (45,sInfo,"filename=""",1) > 0 then
 set oFileInfo=new FileInfo
 ' 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 the 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.add sFormName,oFileInfo
 else
 ' If it is a form item 
 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>

The content described in this article is the most simplified program for uploading files. Please analyze it yourself, learn this drill, and basically master the uploading function like asp1


Related articles: