ASP file Upload: File Upload source code

Source: Internet
Author: User

<!--#include file= "Upload.inc"-->
<%
Dim upload,file,formname,formpath,icount,filename,fileext
Set upload=new upload_5xsoft ' Build upload Object
Formpath= "Uploadimages/"
"Add (/) to the directory
If Right (formpath,1) <> "/" Then formpath=formpath& "/"
Icount=0
For every formName in Upload.file ' lists all uploaded files
Set File=upload.file (formName) ' generates a File object
If File.filesize<100 Then
Response.Write "<font size=3><br> please first select the pictures you want to upload [<a href=# onclick=history.go ( -1) > re-upload </a>] </font> "
Response.End
End If

If file.filesize>1000000 Then
Response.Write "<font size=3><br> picture size exceeded limit [<a href=# onclick=history.go ( -1) > re-upload </a>]</ Font> "
Response.End
End If

Fileext=lcase (Right (file.filename,4))

If fileext<> ". jpg" and fileext<> ". gif" Then
Response.Write "<font size=3><br> file format can only be JPG and GIF format [<a href=# onclick=history.go ( -1) > re-upload </a >]</font> "
Response.End
End If
Randomize
Rannum=int (90000*RND) +10000
Filename=formpath&year (now) &month-&day (now) &hour (now) &minute (now) &second (now) & Fileext
Filename1=year (now) &month (now), &day (now), &hour (now) &minute (now) &second

If file. Filesize>0 Then ' if FileSize > 0 indicates file data
File. SaveAs server.mappath (filename) ' Save file '
Response.Write "<script>parent.form1.img.value= '" &FileName1& "' </script>"

Icount=icount+1
End If
Set file=nothing
Next
Set Upload=nothing ' Deletes this object

Response.Write " '
Response.End
%>

Here is the code for UPLOAD.INC.

&lt;script runat=server language=vbscript&gt;


Dim Upfile_5xsoft_stream


Class Upload_5xsoft


Dim form,file,version


Private Sub Class_Initialize


Dim Istart,ifilenamestart,ifilenameend,iend,vbenter,iformstart,iformend,thefile


Dim Strdiv,mformname,mformvalue,mfilename,mfilesize,mfilepath,idivlen,mstr


if Request.totalbytes&lt;1 then Exit Sub


set Form=createobject ("Scripting.Dictionary")


set File=createobject ("Scripting.Dictionary")


set Upfile_5xsoft_stream=createobject ("ADODB.stream")


upfile_5xsoft_stream.mode=3


upfile_5xsoft_stream.type=1


Upfile_5xsoft_stream.open


Upfile_5xsoft_stream.write Request.BinaryRead (request.totalbytes)


VBENTER=CHR (a) &AMP;CHR (10)


idivlen=instring (1,vbenter) +1


strdiv=substring (1,idivlen)


Iformstart=idivlen


iformend=instring (Iformstart,strdiv)-1


while Iformstart &lt; Iformend


istart=instring (Iformstart, "name=" ")


iend=instring (istart+6, "" "")


mformname=substring (istart+6,iend-istart-6)


ifilenamestart=instring (iend+1, "filename=" ")


if ifilenamestart&gt;0 and Ifilenamestart&lt;iformend then


ifilenameend=instring (ifilenamestart+10, "" "")


mfilename=substring (ifilenamestart+10,ifilenameend-ifilenamestart-10)


istart=instring (ifilenameend+1,vbenter&amp;vbenter)


iend=instring (Istart+4,vbenter&amp;strdiv)


if Iend&gt;istart then


mfilesize=iend-istart-4


Else


mfilesize=0


End If


Set Thefile=new FileInfo


thefile.filename=getfilename (mfilename)


Thefile.filepath=getfilepath (mfilename)


thefile.filesize=mfilesize


thefile.filestart=istart+4


Thefile.formname=formname


File.add Mformname,thefile


Else


istart=instring (iend+1,vbenter&amp;vbenter)


iend=instring (Istart+4,vbenter&amp;strdiv)

If Iend>istart Then
Mformvalue=substring (istart+4,iend-istart-4)
Else
Mformvalue= ""
End If
Form. ADD Mformname,mformvalue
End If

Iformstart=iformend+idivlen
Iformend=instring (Iformstart,strdiv)-1
Wend
End Sub

Private Function subString (thestart,thelen)
 dim i,c,stemp
 upfile_5xsoft_stream.position= TheStart-1
 stemp=
 for i=1 to TheLen
   if Upfile_5xsoft_stream.eos then Exit for
& nbsp;  C=ASCB (Upfile_5xsoft_stream.read (1))
   If C > 127 Then
    if Upfile_ 5xsoft_stream.eos then Exit for
    stemp=stemp&chr (AscW (ChrB (AscB (1))) &CHRB (c))
    i=i+1
   Else
    STEMP=STEMP&CHR (c)
& nbsp;  End If
 next
 substring=stemp
End Function

Private Function instring (thestart,varstr)
 dim i,j,bt,thelen,str
 instring=0
 str= ToByte (VARSTR)
 thelen=lenb (Str)
 for I=thestart to Upfile_5xsoft_stream.size-thelen
    If I>upfile_5xsoft_stream.size then exit Function
   upfile_5xsoft_stream.position=i-1
   if AscB (Upfile_5xsoft_stream.read (1)) =ASCB (MidB (str,1)) then
    instring=i
    for j=2 to TheLen
      if Upfile_5xsoft_stream.eos then
 & nbsp;      instring=0
        Exit for
       End If
      if AscB (Upfile_5xsoft_stream.read (1)) <> AscB (MidB (str,j,1)) then
        instring=0
         Exit for
      End If
    nExt
    If instring<>0 then Exit function
   End If
 next
End Function

Private Sub class_terminate 
  form. RemoveAll
  file. RemoveAll
  Set form=nothing
  set file=nothing
  Upfile_5xsoft_stream.close
  set up File_5xsoft_stream=nothing
End Sub
  
 
 private function GetFilePath (fullpath)
  If fullpath <> "Then
   GetFilePath = Left (Fullpath,instrrev (FullPath," "))
  E LSE
   GetFilePath = "
  end If
 End  function
 
 private function GetFileName (fullpath)
  If fullpath <> "" Then
   GetFileName = Mid (Fullpath,instrrev (FULLP Ath, "") +1
  Else
   getfilename = "
  end If
 End  function

 private function ToByte (Str)
   Dim i,icode,c,ilow,ihigh
   tobyte= ""
 & nbsp For I=1 to Len (Str)
   c=mid (str,i,1)
   icode =asc (c)
   If icode<0 Then iCo De = Icode + 65535
   If icode>255 Then
     ilow = Left (Hex (ASC (c)), 2
  ;    Ihigh =right (Hex (ASC (c)), 2
     tobyte = tobyte & ChrB ("&h" & Ilow) & ChrB ("&h" &ihigh)
   Else
     tobyte = tobyte & ChrB (AscB ( c)
   end If
   Next
 end function
End Class


Class FileInfo
  Dim formname,filename,filepath,filesize,filestart
  Private Sub Class_ Initialize
    FileName = ""
    FilePath = ""
    FileSize = 0< br>     filestart= 0
    FormName = "
  end Sub
 
 public F Unction SaveAs (FullPath)
    Dim dr,errorchar,i
    Saveas=1
     If trim (fullpath) = "" or filesize=0 or filestart=0 or filename= "" Then Exit Function
    if FileS Tart=0 or Right (fullpath,1) = "/" Then Exit Function
    set Dr=createobject ("ADODB.stream")
    Dr. mode=3
    Dr. type=1
    Dr. Open
    upfile_5xsoft_stream.position=filestart-1
    Upfile_5xsoft_ Stream.copyto dr,filesize
    Dr. SaveToFile fullpath,2
   Dr. Close
    set dr=nothing
    saveas=0
  End Function
End Class
;/script>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.