| 29 |
jtweddle |
1 |
<%
|
|
|
2 |
' ------------------------------------------------------------------------------
|
|
|
3 |
' Author: Lewis Moten
|
|
|
4 |
' Email: Lewis@Moten.com
|
|
|
5 |
' URL: http://www.lewismoten.com
|
|
|
6 |
' Date: March 19, 2002
|
|
|
7 |
' ------------------------------------------------------------------------------
|
|
|
8 |
|
|
|
9 |
' Field class represents interface to data passed within one field
|
|
|
10 |
'
|
|
|
11 |
' Two available methods of getting a field:
|
|
|
12 |
' Set objField = objUpload.Fields("File1")
|
|
|
13 |
' Set objField = objUpload("File1")
|
|
|
14 |
'
|
|
|
15 |
'
|
|
|
16 |
' objField.Name
|
|
|
17 |
' Name of the field as defined on the form
|
|
|
18 |
'
|
|
|
19 |
' objFiled.Filepath
|
|
|
20 |
' Path that file was sent from
|
|
|
21 |
'
|
|
|
22 |
' ie: C:\Documents and Settings\lmoten\Desktop\Photo.gif
|
|
|
23 |
'
|
|
|
24 |
' objField.FileDir
|
|
|
25 |
' Directory that file was sent from
|
|
|
26 |
'
|
|
|
27 |
' ie: C:\Documents and Settings\lmoten\Desktop
|
|
|
28 |
'
|
|
|
29 |
' objField.FileExt
|
|
|
30 |
' Uppercase Extension of the file
|
|
|
31 |
'
|
|
|
32 |
' ie: GIF
|
|
|
33 |
'
|
|
|
34 |
' objField.FileName
|
|
|
35 |
' Name of the file
|
|
|
36 |
'
|
|
|
37 |
' use: Response.AddHeader "Content-Disposition", "filename=""" & objField.FileName & """"
|
|
|
38 |
'
|
|
|
39 |
' ie: Photo.gif
|
|
|
40 |
'
|
|
|
41 |
' objField.ContentType
|
|
|
42 |
' Type of binary data
|
|
|
43 |
'
|
|
|
44 |
' use: Response.ContentType = objField.ContentType
|
|
|
45 |
'
|
|
|
46 |
' ie: image/gif
|
|
|
47 |
'
|
|
|
48 |
' objField.Value
|
|
|
49 |
' Unicode value passed from form. This value is empty if the field is binary data.
|
|
|
50 |
'
|
|
|
51 |
' use: Response.Write "The value of this field is: " & objField.Value
|
|
|
52 |
'
|
|
|
53 |
' objField.BinaryData
|
|
|
54 |
' Contents of files binary data. (Integer SubType Array)
|
|
|
55 |
'
|
|
|
56 |
' use: Response.BinaryWrite objField.BinaryData
|
|
|
57 |
'
|
|
|
58 |
' objField.BLOB
|
|
|
59 |
' Same thing as BinaryData but with a shorter name. Added to help prevent
|
|
|
60 |
' confusion with database access.
|
|
|
61 |
'
|
|
|
62 |
' use: Call lobjRs.Fields("Image").AppendChunk(objField.BLOB)
|
|
|
63 |
'
|
|
|
64 |
' objField.Length
|
|
|
65 |
' byte size of Value or BinaryData - depending on type of field
|
|
|
66 |
'
|
|
|
67 |
' use: Response.Write "The size of this file is: " & objField.Length
|
|
|
68 |
'
|
|
|
69 |
' objField.BinaryAsText()
|
|
|
70 |
' Converts binary data into unicode format. Useful when you expect the user
|
|
|
71 |
' to upload a text file and you have the need to interact with it.
|
|
|
72 |
'
|
|
|
73 |
' use: Response.Write objField.BinaryAsText()
|
|
|
74 |
'
|
|
|
75 |
' objField.SaveAs()
|
|
|
76 |
' Saves binary data to a specified path. This will overwrite any existing files.
|
|
|
77 |
'
|
|
|
78 |
' use: objField.SaveAs(Server.MapPath("/Uploads/") & "\" & objField.FileName)
|
|
|
79 |
'
|
|
|
80 |
' ------------------------------------------------------------------------------
|
|
|
81 |
Class clsField
|
|
|
82 |
|
|
|
83 |
Public Name ' Name of the field defined in form
|
|
|
84 |
|
|
|
85 |
Private mstrPath ' Full path to file on visitors computer
|
|
|
86 |
' C:\Documents and Settings\lmoten\Desktop\Photo.gif
|
|
|
87 |
|
|
|
88 |
Public FileDir ' Directory that file existed in on visitors computer
|
|
|
89 |
' C:\Documents and Settings\lmoten\Desktop
|
|
|
90 |
|
|
|
91 |
Public FileExt ' Extension of the file
|
|
|
92 |
' GIF
|
|
|
93 |
|
|
|
94 |
Public FileName ' Name of the file
|
|
|
95 |
' Photo.gif
|
|
|
96 |
|
|
|
97 |
Public ContentType ' Content / Mime type of file
|
|
|
98 |
' image/gif
|
|
|
99 |
|
|
|
100 |
Public Value ' Unicode value of field (used for normail form fields - not files)
|
|
|
101 |
|
|
|
102 |
Public BinaryData ' Binary data passed with field (for files)
|
|
|
103 |
|
|
|
104 |
Public Length ' byte size of value or binary data
|
|
|
105 |
|
|
|
106 |
Private mstrText ' Text buffer
|
|
|
107 |
' If text format of binary data is requested more then
|
|
|
108 |
' once, this value will be read to prevent extra processing
|
|
|
109 |
|
|
|
110 |
' ------------------------------------------------------------------------------
|
|
|
111 |
Public Property Get BLOB()
|
|
|
112 |
BLOB = BinaryData
|
|
|
113 |
End Property
|
|
|
114 |
' ------------------------------------------------------------------------------
|
|
|
115 |
Public Function BinaryAsText()
|
|
|
116 |
|
|
|
117 |
' Binary As Text returns the unicode equivilant of the binary data.
|
|
|
118 |
' this is useful if you expect a visitor to upload a text file that
|
|
|
119 |
' you will need to work with.
|
|
|
120 |
|
|
|
121 |
' NOTICE:
|
|
|
122 |
' NULL values will prematurely terminate your Unicode string.
|
|
|
123 |
' NULLs are usually found within binary files more often then plain-text files.
|
|
|
124 |
' a simple way around this may consist of replacing null values with another character
|
|
|
125 |
' such as a space " "
|
|
|
126 |
|
|
|
127 |
Dim lbinBytes
|
|
|
128 |
Dim lobjRs
|
|
|
129 |
|
|
|
130 |
' Don't convert binary data that does not exist
|
|
|
131 |
If Length = 0 Then Exit Function
|
|
|
132 |
If LenB(BinaryData) = 0 Then Exit Function
|
|
|
133 |
|
|
|
134 |
' If we previously converted binary to text, return the buffered content
|
|
|
135 |
If Not Len(mstrText) = 0 Then
|
|
|
136 |
BinaryAsText = mstrText
|
|
|
137 |
Exit Function
|
|
|
138 |
End If
|
|
|
139 |
|
|
|
140 |
' Convert Integer Subtype Array to Byte Subtype Array
|
|
|
141 |
lbinBytes = ASCII2Bytes(BinaryData)
|
|
|
142 |
|
|
|
143 |
' Convert Byte Subtype Array to Unicode String
|
|
|
144 |
mstrText = Bytes2Unicode(lbinBytes)
|
|
|
145 |
|
|
|
146 |
' Return Unicode Text
|
|
|
147 |
BinaryAsText = mstrText
|
|
|
148 |
|
|
|
149 |
End Function
|
|
|
150 |
' ------------------------------------------------------------------------------
|
|
|
151 |
Public Sub SaveAs(ByRef pstrFileName)
|
|
|
152 |
|
|
|
153 |
Dim lobjStream
|
|
|
154 |
Dim lobjRs
|
|
|
155 |
Dim lbinBytes
|
|
|
156 |
|
|
|
157 |
' Don't save files that do not posess binary data
|
|
|
158 |
If Length = 0 Then Exit Sub
|
|
|
159 |
If LenB(BinaryData) = 0 Then Exit Sub
|
|
|
160 |
|
|
|
161 |
' Create magical objects from never never land
|
|
|
162 |
Set lobjStream = Server.CreateObject("ADODB.Stream")
|
|
|
163 |
|
|
|
164 |
' Let stream know we are working with binary data
|
|
|
165 |
lobjStream.Type = adTypeBinary
|
|
|
166 |
|
|
|
167 |
' Open stream
|
|
|
168 |
Call lobjStream.Open()
|
|
|
169 |
|
|
|
170 |
' Convert Integer Subtype Array to Byte Subtype Array
|
|
|
171 |
lbinBytes = ASCII2Bytes(BinaryData)
|
|
|
172 |
|
|
|
173 |
' Write binary data to stream
|
|
|
174 |
Call lobjStream.Write(lbinBytes)
|
|
|
175 |
|
|
|
176 |
' Save the binary data to file system
|
|
|
177 |
' Overwrites file if previously exists!
|
|
|
178 |
Call lobjStream.SaveToFile(pstrFileName, adSaveCreateOverWrite)
|
|
|
179 |
|
|
|
180 |
' Close the stream object
|
|
|
181 |
Call lobjStream.Close()
|
|
|
182 |
|
|
|
183 |
' Release objects
|
|
|
184 |
Set lobjStream = Nothing
|
|
|
185 |
|
|
|
186 |
End Sub
|
|
|
187 |
' ------------------------------------------------------------------------------
|
|
|
188 |
Public Property Let FilePath(ByRef pstrPath)
|
|
|
189 |
|
|
|
190 |
mstrPath = pstrPath
|
|
|
191 |
|
|
|
192 |
' Parse File Ext
|
|
|
193 |
If Not InStrRev(pstrPath, ".") = 0 Then
|
|
|
194 |
FileExt = Mid(pstrPath, InStrRev(pstrPath, ".") + 1)
|
|
|
195 |
FileExt = UCase(FileExt)
|
|
|
196 |
End If
|
|
|
197 |
|
|
|
198 |
' Parse File Name
|
|
|
199 |
If Not InStrRev(pstrPath, "\") = 0 Then
|
|
|
200 |
FileName = Mid(pstrPath, InStrRev(pstrPath, "\") + 1)
|
|
|
201 |
End If
|
|
|
202 |
|
|
|
203 |
' Parse File Dir
|
|
|
204 |
If Not InStrRev(pstrPath, "\") = 0 Then
|
|
|
205 |
FileDir = Mid(pstrPath, 1, InStrRev(pstrPath, "\") - 1)
|
|
|
206 |
End If
|
|
|
207 |
|
|
|
208 |
End Property
|
|
|
209 |
' ------------------------------------------------------------------------------
|
|
|
210 |
Public Property Get FilePath()
|
|
|
211 |
FilePath = mstrPath
|
|
|
212 |
End Property
|
|
|
213 |
' ------------------------------------------------------------------------------
|
|
|
214 |
Private Function ASCII2Bytes(ByRef pbinBinaryData)
|
|
|
215 |
|
|
|
216 |
Dim lobjRs
|
|
|
217 |
Dim llngLength
|
|
|
218 |
Dim lbinBuffer
|
|
|
219 |
|
|
|
220 |
' get number of bytes
|
|
|
221 |
llngLength = LenB(pbinBinaryData)
|
|
|
222 |
|
|
|
223 |
Set lobjRs = Server.CreateObject("ADODB.Recordset")
|
|
|
224 |
|
|
|
225 |
' create field in an empty recordset to hold binary data
|
|
|
226 |
Call lobjRs.Fields.Append("BinaryData", adLongVarBinary, llngLength)
|
|
|
227 |
|
|
|
228 |
' Open recordset
|
|
|
229 |
Call lobjRs.Open()
|
|
|
230 |
|
|
|
231 |
' Add a new record to recordset
|
|
|
232 |
Call lobjRs.AddNew()
|
|
|
233 |
|
|
|
234 |
' Populate field with binary data
|
|
|
235 |
Call lobjRs.Fields("BinaryData").AppendChunk(pbinBinaryData & ChrB(0))
|
|
|
236 |
|
|
|
237 |
' Update / Convert Binary Data
|
|
|
238 |
' Although the data we have is binary - it has still been
|
|
|
239 |
' formatted as 4 bytes to represent each byte. When we
|
|
|
240 |
' update the recordset, the Integer Subtype Array that we
|
|
|
241 |
' passed into the Recordset will be converted into a
|
|
|
242 |
' Byte Subtype Array
|
|
|
243 |
Call lobjRs.Update()
|
|
|
244 |
|
|
|
245 |
' Request binary data and save to stream
|
|
|
246 |
lbinBuffer = lobjRs.Fields("BinaryData").GetChunk(llngLength)
|
|
|
247 |
|
|
|
248 |
' Close recordset
|
|
|
249 |
Call lobjRs.Close()
|
|
|
250 |
|
|
|
251 |
' Release recordset from memory
|
|
|
252 |
Set lobjRs = Nothing
|
|
|
253 |
|
|
|
254 |
' Return Bytes
|
|
|
255 |
ASCII2Bytes = lbinBuffer
|
|
|
256 |
|
|
|
257 |
End Function
|
|
|
258 |
' ------------------------------------------------------------------------------
|
|
|
259 |
Private Function Bytes2Unicode(ByRef pbinBytes)
|
|
|
260 |
|
|
|
261 |
Dim lobjRs
|
|
|
262 |
Dim llngLength
|
|
|
263 |
Dim lstrBuffer
|
|
|
264 |
|
|
|
265 |
llngLength = LenB(pbinBytes)
|
|
|
266 |
|
|
|
267 |
Set lobjRs = Server.CreateObject("ADODB.Recordset")
|
|
|
268 |
|
|
|
269 |
' Create field in an empty recordset to hold binary data
|
|
|
270 |
Call lobjRs.Fields.Append("BinaryData", adLongVarChar, llngLength)
|
|
|
271 |
|
|
|
272 |
' Open Recordset
|
|
|
273 |
Call lobjRs.Open()
|
|
|
274 |
|
|
|
275 |
' Add a new record to recordset
|
|
|
276 |
Call lobjRs.AddNew()
|
|
|
277 |
|
|
|
278 |
' Populate field with binary data
|
|
|
279 |
Call lobjRs.Fields("BinaryData").AppendChunk(pbinBytes)
|
|
|
280 |
|
|
|
281 |
' Update / Convert.
|
|
|
282 |
' Ensure bytes are proper subtype
|
|
|
283 |
Call lobjRs.Update()
|
|
|
284 |
|
|
|
285 |
' Request unicode value of binary data
|
|
|
286 |
lstrBuffer = lobjRs.Fields("BinaryData").Value
|
|
|
287 |
|
|
|
288 |
' Close recordset
|
|
|
289 |
Call lobjRs.Close()
|
|
|
290 |
|
|
|
291 |
' Release recordset from memory
|
|
|
292 |
Set lobjRs = Nothing
|
|
|
293 |
|
|
|
294 |
' Return Unicode
|
|
|
295 |
Bytes2Unicode = lstrBuffer
|
|
|
296 |
|
|
|
297 |
End Function
|
|
|
298 |
|
|
|
299 |
' ------------------------------------------------------------------------------
|
|
|
300 |
End Class
|
|
|
301 |
' ------------------------------------------------------------------------------
|
|
|
302 |
%>
|