No component ASP file upload source code

Source: Internet
Author: User

Remember to create a folder "updata"

Saveannounce_upload.asp upload page
------------------------------------
<HTML>
<Head>
<Style type = "text/CSS

">
Body {font-size: 9pt ;}
Input {font-size: 9pt ;}
</Style>
<Title> File Upload </title>
</Head>
<Body>
<Form name = "form" method = "Post" Action = "saveannouce_upfile.asp" enctype = "multipart/form-Data">
File
<Input type = "file" name = "file1" size = 10>
<Input type = "Submit" name = "Submit" value = "Upload">
</Form>
</Body>
</Html>

------------------------------------
Saveannouce_upfile.asp save the file to the server


------------------------------------
<! -- # Include file = "Upload. Inc" -->
<HTML>
<Head>
<Title> File Upload </title>
</Head>
<Body>
<%
Dim upload, file, formname, formpath
Set upload = new upload_5xsoft '': creates an upload object.
Formpath = upload. Form ("filepath") ''after the directory is added (/)
If right (formpath, 1) <> "/" then formpath = formpath &"/"
For each formname in upload. file' list all uploaded files
Set file = upload. File (formname) ''to generate a file object
If file. filesize <100 then
Response. write "<font size = 2> select the file you want to upload [<a href = # onclick = history. go (-1)> re-upload </a>] </font>"
Response. End
End if
If file. filesize> 500*1000 then', set the size of the uploaded file to 500 kb.
Response. write "<font size = 2> the file size exceeds the limit of 500 kb [<a href = # onclick = history. go (-1)> re-upload </a>] </font>"
Response. End
End if
If file. filesize> 0 then'' if filesize> 0, file data exists.
File. saveas server. mappath ("updata/" & file. filename) ''save the file
End if
Set file = nothing
Next
Set upload = nothing
Response. write "<font size = 2> the file is uploaded successfully [<a href = # onclick = history. go (-1)> continue upload </a>] </font>"
%>
</Body>
</Html>
------------------------------------
Upload. inc creates an upload object
------------------------------------
<SCRIPT runat = server Language = VBScript>

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
Version = ""
If request. totalbytes <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 (13) & CHR (10)
Idivlen = instring (1, vbenter) + 1
Strdiv = substring (1, idivlen)
Iformstart = idivlen
Iformend = instring (iformstart, strdiv)-1
While iformstart <iformend
Istart = instring (iformstart, "name = """)
Iend = instring (istart + 6 ,"""")
Mformname = substring (istart + 6, iEnd-iStart-6)
Ifilenamestart = instring (iend + 1, "filename = """)
If ifilenamestart> 0 and ifilenamestart <iformend then
Ifilenameend = instring (ifilenamestart + 10 ,"""")
Mfilename = substring (ifilenamestart + 10, iFileNameEnd-iFileNameStart-10)
Istart = instring (ifilenameend + 1, vbenter & vbenter)
Iend = instring (istart + 4, vbenter & strdiv)
If iend> 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 & vbenter)
Iend = instring (istart + 4, vbenter & 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
C = ASCB (upfile_5xsoft_stream.read (1 ))
If C & gt; 127 then
If upfile_5xsoft_stream.eos then exit
Stemp = stemp & CHR (ASCW (chrb (ASCB (upfile_5xsoft_stream.read (1) & chrb (c )))
I = I + 1
Else
Stemp = stemp & CHR (c)
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
Instring = 0
Exit
End if
If ASCB (upfile_5xsoft_stream.read (1) <> ASCB (midb (STR, J, 1) then
Instring = 0
Exit
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 upfile_5xsoft_stream = nothing
End sub

Private function getfilepath (fullpath)
If fullpath <> "" then
Getfilepath = left (fullpath, limit Rev (fullpath ,"/"))
Else
Getfilepath = ""
End if
End Function

Private function getfilename (fullpath)
If fullpath <> "" then
Getfilename = mid (fullpath, limit Rev (fullpath, "/") + 1)
Else
Getfilename = ""
End if
End Function

Private function tobyte (STR)
Dim I, icode, C, ilow, ihigh
Tobyte = ""
For I = 1 to Len (STR)
C = mid (STR, I, 1)
Icode = ASC (c)
If icode <0 then icode = 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
Filestart = 0
Formname = ""
End sub

Public Function saveas (fullpath)
Dim DR, errorchar, I
Saveas = 1
If trim (fullpath) = "" Or filesize = 0 or filestart = 0 or filename = "" Then exit function
If filestart = 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.