-
Notifications
You must be signed in to change notification settings - Fork 0
/
clsField.asp
436 lines (345 loc) · 13 KB
/
clsField.asp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
<%
' ------------------------------------------------------------------------------
' Author: Lewis Moten
' Email: [email protected]
' 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)
'
' objField.DataStart
' location within all posted binary data where particular fields data begins.
'
' use: FieldData = MidB(BinaryData, objField.DataStart, objField.DataLength)
' objField.DataLength
' length of the posted binary data
'
' use: FieldData = MidB(BinaryData, objField.DataStart, objField.DataLength)
' ------------------------------------------------------------------------------
Class clsField
Public Name ' Name of the field defined in form
Private mstrPath ' Full path to file on visitors computer
' C:\Documents and Settings\lmoten\Desktop\Photo.gif
Public FileDir ' Directory that file existed in on visitors computer
' C:\Documents and Settings\lmoten\Desktop
Public FileExt ' Extension of the file
' GIF
Public FileName ' Name of the file
' Photo.gif
Public ContentType ' Content / Mime type of file
' image/gif
' Public 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 data
Private mstrText ' Text buffer
' If text format of binary data is requested more then
' once, this value will be read to prevent extra processing
Private msg ' Error Message
Public DataStart ' Index where field data begins in posted data
Public DataLength ' Length of field data within posted data
Private ProductName
Private ProductVersion
Private ErrorSignature
' ------------------------------------------------------------------------------
Public Property Get BLOB()
BLOB = BinaryData
End Property
' ------------------------------------------------------------------------------
Public Property Get BinaryData()
' Parses binary content of the chunk
If DataStart = 0 Then Exit Property
If DataLength = 0 Then Exit Property
BinaryData = MidB(gBinaryData, DataStart, DataLength)
End Property
' ------------------------------------------------------------------------------
Public Property Get Value()
Value = CStrU(BinaryData)
End Property
' ------------------------------------------------------------------------------
Private Function CStrU(ByRef pstrANSI)
' Converts an ANSI string to Unicode
' Best used for small strings
Dim llngLength ' Length of ANSI string
Dim llngIndex ' Current position
' determine length
llngLength = LenB(pstrANSI)
' Loop through each character
For llngIndex = 1 To llngLength
' Pull out ANSI character
' Get Ascii value of ANSI character
' Get Unicode Character from Ascii
' Append character to results
CStrU = CStrU & Chr(AscB(MidB(pstrANSI, llngIndex, 1)))
Next
End Function
' ------------------------------------------------------------------------------
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 " "
If ContentType = "" Then Exit Function
If DataStart = 0 Then Exit Function
If DataLength = 0 Then Exit Function
' If we previously converted binary to text, return the buffered content
If Not Len(mstrText) = 0 Then
BinaryAsText = mstrText
Exit Function
End If
' Convert Integer Subtype Array to Byte Subtype Array
lbinBytes = ASCII2Bytes(BinaryData)
' Convert Byte Subtype Array to Unicode String
mstrText = Bytes2Unicode(lbinBytes)
' Return Unicode Text
BinaryAsText = mstrText
End Function
' ------------------------------------------------------------------------------
Public Sub SaveAs(ByRef pstrFileName)
If FileSystemObjectEnabled Then
Dim FSO
Dim Folder
Set FSO = CreateObject("Scripting.FileSystemObject")
Folder = FSO.GetParentFolderName(pstrFileName)
' verify folder exists
If Not FSO.FolderExists(Folder) Then
Set FSO = Nothing
Call PublishError(7, "Folder does not exist: " & Folder)
Exit Sub
End If
Dim Drive
Dim AvailableSpace
Drive = FSO.GetDriveName(Folder)
AvailableSpace = FSO.GetDrive(Drive).AvailableSpace
Set FSO = Nothing
If AvailableSpace < DataLength Then
Call PublishError(11, "Not enough free space available.")
Exit Sub
End If
End If
Dim lobjStream
Dim lobjRs
Dim lbinBytes
' Don't save files that do not posess binary data
If DataLength = 0 Then
msg = "Failed to save file: " & FileName & ". "
msg = msg & " No data was found within the file (zero bytes)."
msg = msg & " [Need help? Contact Lewis Moten, [email protected], http://www.lewismoten.com]"
On Error Goto 0
Call Err.Raise(vbObjectError + 4, "Upload Without COM", msg)
End If
' Create magical objects from never never land
Set lobjStream = Server.CreateObject("ADODB.Stream")
' Let stream know we are working with binary data
lobjStream.Type = adTypeBinary
' Open stream
Call lobjStream.Open()
' Convert Integer Subtype Array to Byte Subtype Array
lbinBytes = ASCII2Bytes(BinaryData)
' Write binary data to stream
Call lobjStream.Write(lbinBytes)
' Save the binary data to file system
' Overwrites file if previously exists!
On Error Resume Next
Call lobjStream.SaveToFile(pstrFileName, adSaveCreateOverWrite)
Select Case Err.number
Case 0
' Everything is ok.
On Error Goto 0
Case 3004
msg = "Failed to save file as: " & pstrFileName & ". "
msg = msg & "[[[1]]] Make sure your IUSR_MACHINE account has "
msg = msg & """write"" permission on the folder that you are "
msg = msg & "uploading to. [[[2]]] If your ISP has front "
msg = msg & "page support installed, it may hose permissions."
msg = msg & " If this is the case, you may want to disable "
msg = msg & "the front-page support for your production "
msg = msg & "website or the subfolder you are uploading files"
msg = msg & " to. [[[3]]] Use small files for testing. It "
msg = msg & "could be that the large files are timing out. "
Call PublishError(2, msg)
Case Else
msg = "Failed to save file as: " & pstrFileName & " "
msg = msg & "Unexpected Error. "
msg = msg & "Error Number: " & Err.number
msg = msg & " (0x" & Hex(Err.number) & ") "
msg = msg & "Error Description: " & Err.Description & " "
msg = msg & "Error Source: " & Err.Source
Call PublishError(4, msg)
End Select
On Error Goto 0
' Close the stream object
Call lobjStream.Close()
' Release objects
Set lobjStream = Nothing
End Sub
' ------------------------------------------------------------------------------
Public Property Let FilePath(ByRef pstrPath)
mstrPath = pstrPath
' Parse File Ext
If Not InStrRev(pstrPath, ".") = 0 Then
FileExt = Mid(pstrPath, InStrRev(pstrPath, ".") + 1)
FileExt = UCase(FileExt)
End If
' Parse File Name
If InStrRev(pstrPath, "\") = 0 Then
FileName = pstrPath
Else
FileName = Mid(pstrPath, InStrRev(pstrPath, "\") + 1)
End If
' Parse File Dir
If Not InStrRev(pstrPath, "\") = 0 Then
FileDir = Mid(pstrPath, 1, InStrRev(pstrPath, "\") - 1)
End If
End Property
' ------------------------------------------------------------------------------
Public Property Get FilePath()
FilePath = mstrPath
End Property
' ------------------------------------------------------------------------------
Private Function ASCII2Bytes(ByRef pbinBinaryData)
Dim lobjRs
Dim llngLength
Dim lbinBuffer
' get number of bytes
llngLength = LenB(pbinBinaryData)
Set lobjRs = Server.CreateObject("ADODB.Recordset")
' create field in an empty recordset to hold binary data
Call lobjRs.Fields.Append("BinaryData", adLongVarBinary, llngLength)
' Open recordset
Call lobjRs.Open()
' Add a new record to recordset
Call lobjRs.AddNew()
' Populate field with binary data
Call 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 Array
Call lobjRs.Update()
' Request binary data and save to stream
lbinBuffer = lobjRs.Fields("BinaryData").GetChunk(llngLength)
' Close recordset
Call lobjRs.Close()
' Release recordset from memory
Set lobjRs = Nothing
' Return Bytes
ASCII2Bytes = lbinBuffer
End Function
' ------------------------------------------------------------------------------
Private Function Bytes2Unicode(ByRef pbinBytes)
Dim lobjRs
Dim llngLength
Dim lstrBuffer
llngLength = LenB(pbinBytes)
Set lobjRs = Server.CreateObject("ADODB.Recordset")
' Create field in an empty recordset to hold binary data
Call lobjRs.Fields.Append("BinaryData", adLongVarChar, llngLength)
' Open Recordset
Call lobjRs.Open()
' Add a new record to recordset
Call lobjRs.AddNew()
' Populate field with binary data
Call lobjRs.Fields("BinaryData").AppendChunk(pbinBytes)
' Update / Convert.
' Ensure bytes are proper subtype
Call lobjRs.Update()
' Request unicode value of binary data
lstrBuffer = lobjRs.Fields("BinaryData").Value
' Close recordset
Call lobjRs.Close()
' Release recordset from memory
Set lobjRs = Nothing
' Return Unicode
Bytes2Unicode = lstrBuffer
End Function
' ------------------------------------------------------------------------------
Private Sub Class_Initialize()
ProductName = "Upload Without COM"
ProductVersion = "3.0"
ErrorSignature = "[Need help? Contact Lewis Moten, [email protected], http://www.lewismoten.com]"
End Sub
' ------------------------------------------------------------------------------
Private Sub PublishError(number, message)
' writes out error in a specific format.
On Error Goto 0
Call Err.Raise(vbObjectError + number, ProductName & " " & ProductVersion, message + ErrorSignature)
End Sub
' ------------------------------------------------------------------------------
End Class
' ------------------------------------------------------------------------------
%>