ASP no component uploads 1.upload.inc.asp
<% DIM OPFISTREAM CLASS UPFILE_CLASS DIM FORM, FILE, VERSION, ERR
Private sub class_initialize version = "No components Upload class Version v1.0" Err = -1 End Sub
Private sub class_terminate 'Clear Variables and Of Err
Public Sub GetData (RetSize) 'defines a variable Dim RequestBinDate, sSpace, bCrLf, sInfo, iInfoStart, iInfoEnd, tStream, iStart, oFileInfo Dim iFileSize, sFilePath, sFileType, sFormValue, sFileName Dim iFindStart, iFindEnd Dim iFormStart, iFormEnd, sFormName' code begins If Request.TotalBytes <1 THEN Err = 1 EXIT SUB End if ife = 1 EXIT SUB End 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 (13) & ChrB (10) 'each made Separator SSCE = MIDB (Requestbindate, 1, INSTRB (1, RequestBindate, BCRLF) - ISTART = lenb (SSP ace) iFormStart = iStart 2 'exploded item Do 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 'item name acquired form iFormStart = InStrB (iInfoEnd, RequestBinDate, sSpace) -1 iFindStart = InStr (22, sInfo, "name = "" ", 1) 6 ifindend = INSTR (ifindstart, sinfo," "" ", 1) sformname =
MID (SINFO, IFINDSTART) 'If it is file IF INSTR (45, Sinfo, "FileName =", ", 1)> 0 THEN SET OFLOLINFO = New fileInfo_class' get file attribute ifindstart = instr (ifindend, sinfo, "FileName =" ", 1) 10 ifindend = INSTR (ifindstart, sinfo," "", 1) sfilename = MID (SINFO, IFINDSTART, IFINDENDSTART) Ofileinfo.fileName = MID (SFileName, Instrrev (sfilename, "/") 1) ofileinfo.filepath = left (sfilename, "/") 1) ofileinfo.fileExt = MID (sfilename, INSTRREV (sfilename, ".") 1) ifindset = 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 'form if the project tStream.Close tStream.Type = 1 tStream.Mode = 3 tStream.Open oUpFileStream.Position = iInfoEnd oUpFileStream.CopyTo tStream, iFormStart-iInfoEnd-2 TSTRE am.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 the file ends, exiting Loop Until (IFORMSTART 2) = iformend requestbindate = "" SET TSTREAM = Nothing End Subend Class
'File attribute class Class FileInfo_ClassDim FormName, FileName, FilePath, FileSize, FileType, FileStart, FileExt' methods for saving files Public Function SaveToFile (Path) On Error Resume NextDim oFileStreamSet oFileStream = CreateObject ( "ADODB.Stream") = 1oFileStream oFileStream.Type. Mode = 3oFileStream.OpenoUpFileStream.Position = FileStartoUpFileStream.CopyTo oFileStream, FileSizeoFileStream.SaveToFile Path, 2oFileStream.CloseSet oFileStream = Nothingif Err.Number <> 0 thenSaveToFile = err.number & "**" & Err.descriptonelseSaveToFile = "ok" end ifEnd Function ' Obending file data public function filedateoupFileStream.position = filestRedate = OupfileStream.read (filesis) End Functionend Class%>
2. Handle the page after the user submitted (UPLOAD.ASP)
<% DIM UPLOAD, FILE, FORMNAME, FORMPATH, FILENAME, FILEEXTDIM RANNUMCALL UPFILE () '=========== No component upload (UPLOAD_0) ============== ====== Sub upfile () set upload = new updile_class 'Establish upload object Upload.getdata (500 * 1024)' get uploaded data, here is 500 k
If UPLOAD.ERR> 0 THENSELECT CASE UPLOAD.ERRCASE 1RESPONSE.WRITE "Please select the file you want to upload []" End SelectExit SubelseFormPath = UPLOAD.FORM ("FilePath") 'file Save Directory, This directory must be readwritten if FormPath = "" "" = "rwdata /" end "after the directory (/) IF Right (FormPath, 1) <>" / "THEN FORMPATH = FormPath &" / "end if FOR Each Formname In Upload.file 'lists all uploaded files set file = upload.file (formname)' Generates a file object if file.filesize <100 TenResponse.write "please select the picture you want to upload [ Re-upload ] "Response.Endend ifxt = LCASE (file.fileext) if checkfileext (fileext) = false" = false "file format is incorrect [ Re-upload ] "Response.Endend IF 'RandomizeranNum = int (90000 * rnd) 10000filename = formPath & year (now) & month (now) & day (now) & hour (now) & minute (now) & second (now) & ranNum & "." & FileExtif file.FileSize> 0 then' if FileSize > 0 Description Document Data Result = file.savetofile (server.mappath (filename)) 'Save File if Result = "OK" Theresponse.write Formname & "Upload Ok, Had Saved to" & FileName & " 'Determines whether the file type qualified Private Function CheckFileExt (fileEXT) dim ForumuploadForumupload = "gif, jpg, bmp, jpeg" Forumupload = split (Forumupload, ",") for i = 0 to ubound (Forumupload) if lcase (fileEXT) = lcase (Trim (Forumupload (I))) ThencheckfileExt = trueXit FunctionElseCheckFileExt = false IfNextend Function%> TD> TR> table> body> html> 3.HTML form (UPLOAD.HTML) -> form> body> html> |