%
Const FileSystemObjectEnabled = True
' If your ISP does not allow the File System Object to
' be used, then set this value to false. Some features
' will be disabled such as verifying folders exist,
' Assigning unique names to files, displaying progress,
' and interacting with existing files (Move, Copy, Delete,
' Rename)
Const BufferSize = &H10000
' Changing buffer size may change the length of time
' it takes to upload a file. You may want to begin
' with 64KB and go from there to find the optimal
' number for your website.
' Since the Progress Information class writes to
' a file each itteration, this can degrade performance
' a lot when using small buffers.
' Do not go below 100 bytes, or you will begin to risk
' not being able to parse boundaries. Data may not upload
' properly.
' For your reference:
' 1 KB 1024 &H400
' 2 KB 2048 &H800
' 4 KB 4096 &H1000
' 8 KB 8192 &H2000
' 16 KB 16384 &H4000
' 32 KB 32768 &H8000
' 64 KB 65536 &H10000
' 128 KB 131072 &H20000
' 256 KB 262144 &H40000
%>
<%
' ------------------------------------------------------------------------------
' Author: Lewis Moten
' Email: Lewis@Moten.com
' URL: http://www.lewismoten.com
' Date: September 1, 2003
' ------------------------------------------------------------------------------
' Upload class retrieves multi-part form data posted to web page
' and parses it into objects that are easy to interface with.
' Requires MDAC (ADODB) COM components found on most servers today
' Additional compenents are not necessary.
'
' Demo:
' Set objUpload = new clsUpload
' Initializes object and parses all posted multi-part from data.
' Once this as been done, Access to the Request object is restricted
'
' objUpload.Count
' Number of fields retrieved
'
' use: Response.Write "There are " & objUpload.Count & " fields."
'
' objUpload.Fields
' Access to field objects. This is the default propert so it does
' not necessarily have to be specified. You can also determine if
' you wish to specify the field index, or the field name.
'
' Use:
' Set objField = objUpload.Fields("File1")
' Set objField = objUpload("File1")
' Set objField = objUpload.Fields(0)
' Set objField = objUpload(0)
' Response.Write objUpload("File1").Name
' Response.Write objUpload(0).Name
'
' ------------------------------------------------------------------------------
'
' List of all fields passed:
'
' For i = 0 To objUpload.Count - 1
' Response.Write objUpload(i).Name & "
"
' Next
'
' ------------------------------------------------------------------------------
'
' HTML needed to post multipart/form-data
'
'
' ------------------------------------------------------------------------------
'
' Customized Errors:
' (vbObjectError + ##)
'
' 1: Object does not exist within the ordinal reference.
' 2: Failed to save file ... common reasons
' 3: Failed to parse posted binary data delimiter
' 4: Failed to save file ... unknown
' 5: Used Request.Form ... Failed to read posted form data
' 6: Failed to read posted form data for unknown reason.
' 7: Folder does not exist.
' 8: Filename is not valid
' 9: Folder is not valid
' 10: ADODB.Version below 2.5
' 11: Not enough free space available.
' 12: File System Object has been disabled.
' 13: multipart/form-data was not received.
' ------------------------------------------------------------------------------
'
Dim gBinaryData ' bytes visitor sent to server with posted form data
' Page Scope accessable to both clsUpload and clsFile
Class clsUpload
' ------------------------------------------------------------------------------
Private TotalBytes ' Number of bytes client is sending
Private Delimiter ' Delimiter between multipart/form-data (43 chars)
Private CR ' ANSI Carriage Return
Private LF ' ANSI Line Feed
Private CRLF ' ANSI Carriage Return & Line Feed
Private mobjFieldAry() ' Array to hold field objects
Private mlngCount ' Number of fields parsed
Private msg ' Error Message
Private ProductName ' Name of the product
Private ProductVersion ' Version of the product
Private ErrorSignature ' Signature applied to all products.
Private Progress ' Progress information class
Private ParsedData ' Did we parse the data?
' ------------------------------------------------------------------------------
Private Sub RequestData
If ParsedData Then Exit Sub
ParsedData = True
'On Error Resume Next
' Determine number bytes visitor sent
TotalBytes = Request.TotalBytes
Dim ChunkSize
Dim Received
Dim TotalBytes
Dim BinaryStream
ChunkSize = BufferSize ' Global Property
TotalBytes = Request.TotalBytes
Received = 0
Set BinaryStream = CreateObject("ADODB.Stream")
BinaryStream.Mode = adModeReadWrite
BinaryStream.Type = adTypeBinary
BinaryStream.Open
Do While ChunkSize > 0
' If chunk size buffer will read past the end of the stream
' adjust it to read to the end of the stream.
If ChunkSize + Received > TotalBytes Then ChunkSize = TotalBytes - Received
' get out of the loop if no more data can be read.
If ChunkSize = 0 Then Exit Do
' Get the current chunk
' Write chunk to stream
BinaryStream.Write(Request.BinaryRead(ChunkSize))
' Incriment bytes received
Received = Received + ChunkSize
' As long as the user is still connected ...
If Response.IsClientConnected() Then
' Update Progress information
Progress.LastActive = Now()
Progress.BytesReceived = Received
Call Progress.Save()
Else
' Update Progress information
Progress.UploadCompleted = Now()
Call Progress.Save()
' Stop execution.
Exit Sub
End If
Loop
BinaryStream.Position = 0
gBinaryData = BinaryStream.Read(adReadAll)
BinaryStream.Close
Set BinaryStream = Nothing
' Parse out the delimiter
Delimiter = ParseBoundary()
' Parse the data
Call ParseData
End Sub
' ------------------------------------------------------------------------------
' Private Function ParseDelimiter()
'
' ' Delimiter seperates multiple pieces of form data
' ' "around" 43 characters in length
' ' next character afterwards is carriage return (except last line has two --)
' ' first part of delmiter is dashes followed by hex number
' ' hex number is possibly the browsers session id?
'
' ' Need a MAC to find out why this causes problems.
'
' ' MSIE 3.01 and 3.02 on the Mac, for instance, don't use a
' ' leading '--' in the boundary field for multipart/form-data POSTs
'
' ' Examples:
'
' ' -----------------------------7d230d1f940246
' ' -----------------------------7d22ee291ae0114
'
' ' If we can not find a carriage return and line feed combination ...
' If InStrB(1, gBinaryData, CRLF) = 0 Then
'
' ' We can not determine the delimiter
'
' msg = "Failed to parse posted binary data delimiter. "
' msg = msg & " Make sure your encoding attiribute is set to"
' msg = msg & " mutlipart/form-data in your