Rev 29 | Blame | Compare with Previous | Last modification | View Log | RSS feed
<%' ------------------------------------------------------------------------------' Author: Lewis Moten' Email: Lewis@Moten.com' URL: http://www.lewismoten.com' Date: March 19, 2002' ------------------------------------------------------------------------------' Field class represents interface to data passed within one field'' Two available methods of getting a field:' Set objField = objUpload.Fields("File1")' Set objField = objUpload("File1")''' objField.Name' Name of the field as defined on the form'' objFiled.Filepath' Path that file was sent from'' ie: C:\Documents and Settings\lmoten\Desktop\Photo.gif'' objField.FileDir' Directory that file was sent from'' ie: C:\Documents and Settings\lmoten\Desktop'' objField.FileExt' Uppercase Extension of the file'' ie: GIF'' objField.FileName' Name of the file'' use: Response.AddHeader "Content-Disposition", "filename=""" & objField.FileName & """"'' ie: Photo.gif'' objField.ContentType' Type of binary data'' use: Response.ContentType = objField.ContentType'' ie: image/gif'' objField.Value' Unicode value passed from form. This value is empty if the field is binary data.'' use: Response.Write "The value of this field is: " & objField.Value'' objField.BinaryData' Contents of files binary data. (Integer SubType Array)'' use: Response.BinaryWrite objField.BinaryData'' objField.BLOB' Same thing as BinaryData but with a shorter name. Added to help prevent' confusion with database access.'' use: Call lobjRs.Fields("Image").AppendChunk(objField.BLOB)'' objField.Length' byte size of Value or BinaryData - depending on type of field'' use: Response.Write "The size of this file is: " & objField.Length'' objField.BinaryAsText()' Converts binary data into unicode format. Useful when you expect the user' to upload a text file and you have the need to interact with it.'' use: Response.Write objField.BinaryAsText()'' objField.SaveAs()' Saves binary data to a specified path. This will overwrite any existing files.'' use: objField.SaveAs(Server.MapPath("/Uploads/") & "\" & objField.FileName)'' ------------------------------------------------------------------------------Class clsFieldPublic Name ' Name of the field defined in formPrivate mstrPath ' Full path to file on visitors computer' C:\Documents and Settings\lmoten\Desktop\Photo.gifPublic FileDir ' Directory that file existed in on visitors computer' C:\Documents and Settings\lmoten\DesktopPublic FileExt ' Extension of the file' GIFPublic FileName ' Name of the file' Photo.gifPublic ContentType ' Content / Mime type of file' image/gifPublic Value ' Unicode value of field (used for normail form fields - not files)Public BinaryData ' Binary data passed with field (for files)Public Length ' byte size of value or binary dataPrivate mstrText ' Text buffer' If text format of binary data is requested more then' once, this value will be read to prevent extra processing' ------------------------------------------------------------------------------Public Property Get BLOB()BLOB = BinaryDataEnd Property' ------------------------------------------------------------------------------Public Function BinaryAsText()' Binary As Text returns the unicode equivilant of the binary data.' this is useful if you expect a visitor to upload a text file that' you will need to work with.' NOTICE:' NULL values will prematurely terminate your Unicode string.' NULLs are usually found within binary files more often then plain-text files.' a simple way around this may consist of replacing null values with another character' such as a space " "Dim lbinBytesDim lobjRs' Don't convert binary data that does not existIf Length = 0 Then Exit FunctionIf LenB(BinaryData) = 0 Then Exit Function' If we previously converted binary to text, return the buffered contentIf Not Len(mstrText) = 0 ThenBinaryAsText = mstrTextExit FunctionEnd If' Convert Integer Subtype Array to Byte Subtype ArraylbinBytes = ASCII2Bytes(BinaryData)' Convert Byte Subtype Array to Unicode StringmstrText = Bytes2Unicode(lbinBytes)' Return Unicode TextBinaryAsText = mstrTextEnd Function' ------------------------------------------------------------------------------Public Sub SaveAs(ByRef pstrFileName)Dim lobjStreamDim lobjRsDim lbinBytes' Don't save files that do not posess binary dataIf Length = 0 Then Exit SubIf LenB(BinaryData) = 0 Then Exit Sub' Create magical objects from never never landSet lobjStream = Server.CreateObject("ADODB.Stream")' Let stream know we are working with binary datalobjStream.Type = adTypeBinary' Open streamCall lobjStream.Open()' Convert Integer Subtype Array to Byte Subtype ArraylbinBytes = ASCII2Bytes(BinaryData)' Write binary data to streamCall lobjStream.Write(lbinBytes)' Save the binary data to file system' Overwrites file if previously exists!Call lobjStream.SaveToFile(pstrFileName, adSaveCreateOverWrite)' Close the stream objectCall lobjStream.Close()' Release objectsSet lobjStream = NothingEnd Sub' ------------------------------------------------------------------------------Public Property Let FilePath(ByRef pstrPath)mstrPath = pstrPath' Parse File ExtIf Not InStrRev(pstrPath, ".") = 0 ThenFileExt = Mid(pstrPath, InStrRev(pstrPath, ".") + 1)FileExt = UCase(FileExt)End If' Parse File NameIf Not InStrRev(pstrPath, "\") = 0 ThenFileName = Mid(pstrPath, InStrRev(pstrPath, "\") + 1)End If' Parse File DirIf Not InStrRev(pstrPath, "\") = 0 ThenFileDir = Mid(pstrPath, 1, InStrRev(pstrPath, "\") - 1)End IfEnd Property' ------------------------------------------------------------------------------Public Property Get FilePath()FilePath = mstrPathEnd Property' ------------------------------------------------------------------------------Private Function ASCII2Bytes(ByRef pbinBinaryData)Dim lobjRsDim llngLengthDim lbinBuffer' get number of bytesllngLength = LenB(pbinBinaryData)Set lobjRs = Server.CreateObject("ADODB.Recordset")' create field in an empty recordset to hold binary dataCall lobjRs.Fields.Append("BinaryData", adLongVarBinary, llngLength)' Open recordsetCall lobjRs.Open()' Add a new record to recordsetCall lobjRs.AddNew()' Populate field with binary dataCall lobjRs.Fields("BinaryData").AppendChunk(pbinBinaryData & ChrB(0))' Update / Convert Binary Data' Although the data we have is binary - it has still been' formatted as 4 bytes to represent each byte. When we' update the recordset, the Integer Subtype Array that we' passed into the Recordset will be converted into a' Byte Subtype ArrayCall lobjRs.Update()' Request binary data and save to streamlbinBuffer = lobjRs.Fields("BinaryData").GetChunk(llngLength)' Close recordsetCall lobjRs.Close()' Release recordset from memorySet lobjRs = Nothing' Return BytesASCII2Bytes = lbinBufferEnd Function' ------------------------------------------------------------------------------Private Function Bytes2Unicode(ByRef pbinBytes)Dim lobjRsDim llngLengthDim lstrBufferllngLength = LenB(pbinBytes)Set lobjRs = Server.CreateObject("ADODB.Recordset")' Create field in an empty recordset to hold binary dataCall lobjRs.Fields.Append("BinaryData", adLongVarChar, llngLength)' Open RecordsetCall lobjRs.Open()' Add a new record to recordsetCall lobjRs.AddNew()' Populate field with binary dataCall lobjRs.Fields("BinaryData").AppendChunk(pbinBytes)' Update / Convert.' Ensure bytes are proper subtypeCall lobjRs.Update()' Request unicode value of binary datalstrBuffer = lobjRs.Fields("BinaryData").Value' Close recordsetCall lobjRs.Close()' Release recordset from memorySet lobjRs = Nothing' Return UnicodeBytes2Unicode = lstrBufferEnd Function' ------------------------------------------------------------------------------End Class' ------------------------------------------------------------------------------%>