ASP no component File upload instance

Source: Internet
Author: User
Tags exit file upload variables save file
Upload | No component 1. library file (upload.inc.asp)
<%
Dim Oupfilestream

Class Upfile_class

Dim Form,file,version,err

Private Sub Class_Initialize
Version = "No component upload class Version V1.0"
ERR =-1
End Sub

Private Sub Class_Terminate
' Erase variables and the like
If ERR < 0 Then
Form.removeall
Set Form = Nothing
File.removeall
Set File = Nothing
Oupfilestream.close
Set Oupfilestream = Nothing
End If
End Sub

Public Sub GetData (retsize)
' Define variables
Dim Requestbindate,sspace,bcrlf,sinfo,iinfostart,iinfoend,tstream,istart,ofileinfo
Dim Ifilesize,sfilepath,sfiletype,sformvalue,sfilename
Dim Ifindstart,ifindend
Dim Iformstart,iformend,sformname
' Code starts
If Request.TotalBytes < 1 Then
ERR = 1
Exit Sub
End If
If retsize > 0 Then
If request.totalbytes > Retsize Then
ERR = 2
Exit Sub
End If
End If
Set Form = Server.CreateObject ("Scripting.Dictionary")
Form.comparemode = 1
Set File = Server.CreateObject ("Scripting.Dictionary")
File.comparemode = 1
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)
oupfilestream.position = 0
Requestbindate = Oupfilestream.read
Iformend = Oupfilestream.size
Bcrlf = ChrB (+) & ChrB (10)
' Get the separator between each item
Sspace = MidB (requestbindate,1, InStrB (1,REQUESTBINDATE,BCRLF)-1)
IStart = LenB (sspace)
Iformstart = istart+2
' Explode project
Todo
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 form Item name
Iformstart = InStrB (iinfoend,requestbindate,sspace)-1
Ifindstart = InStr (22,sinfo, "name=" "", 1) +6
Ifindend = InStr (Ifindstart,sinfo, "" "", 1)
Sformname = Mid (Sinfo,ifindstart,ifindend-ifindstart)
' If it's a file
If InStr (45,sinfo, "filename=" "", 1) > 0 Then
Set ofileinfo = new Fileinfo_class
' Get file properties
Ifindstart = InStr (Ifindend,sinfo, "filename=" "", 1) +10
Ifindend = InStr (Ifindstart,sinfo, "" "", 1)
sFileName = Mid (Sinfo,ifindstart,ifindend-ifindstart)
Ofileinfo.filename = Mid (Sfilename,instrrev (sfilename, "\") +1)
Ofileinfo.filepath = Left (Sfilename,instrrev (sfilename, "\") +1)
Ofileinfo.fileext = Mid (Sfilename,instrrev (sFileName, ".") +1)
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-2
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-2
tstream.position = 0
Tstream.type = 2
Tstream.charset = "gb2312"
Sformvalue = Tstream.readtext
If form.exists (sformname) Then
Form (sformname) = form (Sformname) & "," & Sformvalue
Else
Form. ADD Sformname,sformvalue
End If
End If
Tstream.close
Iformstart = iformstart+istart+2
' If you get to the end of the file, quit.
Loop Until (iformstart+2) = Iformend
Requestbindate = ""
Set Tstream = Nothing
End Sub
End Class

' File attribute Class
Class Fileinfo_class
Dim Formname,filename,filepath,filesize,filetype,filestart,fileext
' Save File Method
Public Function SaveToFile (Path)
On Error Resume Next
Dim Ofilestream
Set Ofilestream = CreateObject ("ADODB. Stream ")
Ofilestream.type = 1
Ofilestream.mode = 3
Ofilestream.open
Oupfilestream.position = Filestart
Oupfilestream.copyto ofilestream,filesize
Ofilestream.savetofile path,2
Ofilestream.close
Set Ofilestream = Nothing
If Err.number<>0 Then
savetofile=err.number& "* *" &err.descripton
Else
Savetofile= "OK"
End If
End Function

' Get File data
Public Function Filedate
Oupfilestream.position = Filestart
Filedate = Oupfilestream.read (FileSize)
End Function
End Class
%>


2. Process the user submitted page (upload.asp)
<!--#include file= "upload.inc.asp"-->
<title> File Upload </title>
<body topmargin= "0" leftmargin= "0" >
<table width=100% border=0 cellspacing= "0" cellpadding= "0" ><tr><td class=tablebody1 width=100% height= 100% >
<%
Dim upload,file,formname,formpath,filename,fileext
Dim rannum
Call Upfile ()
' =========== no component upload (UPLOAD_0) ====================
Sub Upfile ()
Set upload=new upfile_class ' Build upload Object
Upload. GetData (500*1024) ' Get upload data, here is K

If Upload.err > 0 Then
Select Case Upload.err
Case 1
Response.Write "Please select the file you want to upload first [<a href=# onclick=history.go ( -1) > re-upload </a>]"
Case 2
Response.Write "Picture size exceeds limit K [<a href=# onclick=history.go ( -1) > re-upload </a>]"
End Select
Exit Sub
Else
Formpath=upload.form ("filepath") ' File save directory, this directory must be read/write by program
If Formpath= "" Then
Formpath= "rwdata/"
End If
' Add (/) in the catalogue
If Right (formpath,1) <> "/" Then
formpath=formpath& "/"
End If

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 "Please select the picture you want to upload first [<a href=# onclick=history.go ( -1) > re-upload </a>]"
Response.End
End If

Fileext=lcase (file. Fileext)
If Checkfileext (fileext) =false Then
Response.Write "file format is incorrect [<a href=# onclick=history.go ( -1) > re-upload </a>]"
Response.End
End If

' Randomize
Rannum=int (90000*rnd) +10000
Filename=formpath&year (now) &month (now) &day (now) &hour (now) &minute-&second (now) &ranNum& "." &fileext
If file. Filesize>0 Then ' if FileSize > 0 indicates file data
Result=file. SaveToFile (Server.MapPath (filename)) ' Save file
if result= ' OK ' then
Response.Write formname& ' upload OK, had Saved to &filename& <br>
Else
Response.Write formname& "Upload Fail," &result& " <br>
End If
End If
Set file=nothing
Next
Set upload=nothing
End If
End Sub

' Determine if the file type is qualified
Private Function Checkfileext (fileext)
Dim forumupload
Forumupload= "Gif,jpg,bmp,jpeg"
Forumupload=split (Forumupload, ",")
For i=0 to UBound (forumupload)
If LCase (fileext) =lcase (Trim (forumupload (i)) then
Checkfileext=true
Exit Function
Else
Checkfileext=false
End If
Next
End Function
%>
</td></tr></table>
</body>



3.HTML Forms (upload.html)
<! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" >
<meta http-equiv= "Content-type" content= "text/html; charset=gb2312 ">
<title>asp No component upload </title>

<body>
Please select the file to upload
<form action= "upfile.asp" method= "post" enctype= "Multipart/form-data" Name= "Form1" >
<input type= "File" name= "file" >
<!--<br>
<input type= "File" name= "file" >
<br>
<input type= "File" name= "file" >
<br>-->
<input type= "Submit" name= "Submission" value= "submitted" >
</form>
</body>



Related Article

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.