Well I was put into a situation where I could not use a File Upload component, so I had to come up with a way to process and save files using VBScript.
Following is my code.. feel free to use it.. Stores all
<%
'----------------------------------------------'
' VBS FILEUPLOAD
' By Gorkem Yuksel
' Created: Aug. 15, 2002
'
' Usage:
' call ParseBinary
'
' (Parses form input and stores all
' values into a dictionary object)
'----------------------------------------------'
dim Binary
set FileName = Server.CreateObject("Scripting.Dictionary"
set File = Server.CreateObject("Scripting.Dictionary"
set Frm = server.CreateObject("Scripting.Dictionary"
FileThumbPath = Server.MapPath ("thumbs"
Function bstr(UStr)
'Unicode string to Byte string conversion
Dim lngLoop
Dim strChar
UStr2Bstr = ""
For lngLoop = 1 to Len(UStr)
strChar = Mid(UStr, lngLoop, 1)
bstr = bstr & ChrB(AscB(strChar))
Next
End Function
Function StrA(UStr)
'Byte string to Unicode string conversion
Dim lngLoop
Dim strChar
UStr2Bstr = ""
For lngLoop = 1 to Lenb(UStr)
strChar = Midb(UStr, lngLoop, 1)
stra = stra & Chr(AscB(strChar))
Next
End Function
sub ParseBinary()
' get Binary Form
bl = Request.TotalBytes
Binary = Request.BinaryRead (bl)
' Find Separator
linepos = instrb(1,Binary,chrb(13),0)
Separator = leftb(Binary,linepos - 1)
currpos = linepos
' Get Chuncks
done = false
while done = false
'get Main Chunk
chunkend = instrb(currpos+lenb(separator) + 1,Binary,Separator,0)
if chunkend <> 0 then
if currpos > 50 then
chunk = midb(Binary,currpos + lenb(separator),chunkend-currpos)
else
chunk = midb(Binary,currpos + 1,chunkend-currpos)
end if
currpos = chunkend
'Process Chunk
DoneChunk = false
while DoneChunk = false
' ChunkLine1 contains Form/File information (ie. FileName, FormField Name)
linepos = instrb(2,chunk,chrb(13),0)
C = midb(Chunk,2,linepos)
ChunkLine1 = StrA(C)
'Is this a file? If so, get filename
linepos2 = instrb(linepos+1,chunk,chrb(13),0)
C = midb(Chunk,linepos+1,linepos2-linepos)
ChunkLine2 = StrA(C)
' if chunkline2 contains text, it is a binary file..
if len(chunkline2) > 3 then
isFile = true
else
isFile = false
end if
if isFile = false then
'Get Form Input
linepos = instrb(linepos2+1,chunk,chrb(13),0)
C = midb(Chunk,linepos2+1,linepos-linepos2)
ChunkLine3 = StrA(C)
else
' Get Binary File
linepos = instrb(linepos2+10,chunk,Separator,0)
C = midb(Chunk,linepos2+3,linepos-linepos2-3)
ChunkLine3 = C
end if
if isFile = false then
' Store Form Fields and values into FRM Dictionary object
x = split (ChunkLine1,"name="""
formName = ""
FormName = left(x(1),len(x(1))-3)
FormValue = ""
FormValue = left(ChunkLine3,len(chunkline3))
frm.Add FormName,FormValue
else
'Store File information and Binary into FILE and FILENAME Dictionary objects
x = split (ChunkLine1,"filename="""
y = split (x(0),"name="""
FormName = ""
FormName = left(y(1),len(y(1))-3)
FormValue = ""
FormValue = left(x(1),len(x(1))-3)
formValue = right(FormValue,len(FormValue)-instrRev(FormValue,"\"
)
fileName.Add FormName,FormValue
file.Add FormName,ChunkLine3
end if
donechunk = true
wend
else
done = true
end if
wend
end sub
call ParseBinary
'Process Files
'save file(s) to directory
for each x in FileName.Keys
if x = "ThumbFile" then
'This image is a thumbnail image
set fs = server.CreateObject ("Scripting.FileSystemObject"
set fso = fs.CreateTextFile (FileThumbPath & "\" & FileName.Item(x),true)
For lngLoop = 2 to LenB(File.Item(x))
fso.Write Chr(AscB(MidB(File.Item(x), lngLoop, 1)))
Next
fso.close
set fso = nothing
set fs = nothing
Response.Write "Saved Image: " & filename.Item(x)
Response.Write "<p><img src=""thumbs/" & filename.Item(x) & """>"
end if
next
Following is my code.. feel free to use it.. Stores all
<%
'----------------------------------------------'
' VBS FILEUPLOAD
' By Gorkem Yuksel
' Created: Aug. 15, 2002
'
' Usage:
' call ParseBinary
'
' (Parses form input and stores all
' values into a dictionary object)
'----------------------------------------------'
dim Binary
set FileName = Server.CreateObject("Scripting.Dictionary"
set File = Server.CreateObject("Scripting.Dictionary"
set Frm = server.CreateObject("Scripting.Dictionary"
FileThumbPath = Server.MapPath ("thumbs"
Function bstr(UStr)
'Unicode string to Byte string conversion
Dim lngLoop
Dim strChar
UStr2Bstr = ""
For lngLoop = 1 to Len(UStr)
strChar = Mid(UStr, lngLoop, 1)
bstr = bstr & ChrB(AscB(strChar))
Next
End Function
Function StrA(UStr)
'Byte string to Unicode string conversion
Dim lngLoop
Dim strChar
UStr2Bstr = ""
For lngLoop = 1 to Lenb(UStr)
strChar = Midb(UStr, lngLoop, 1)
stra = stra & Chr(AscB(strChar))
Next
End Function
sub ParseBinary()
' get Binary Form
bl = Request.TotalBytes
Binary = Request.BinaryRead (bl)
' Find Separator
linepos = instrb(1,Binary,chrb(13),0)
Separator = leftb(Binary,linepos - 1)
currpos = linepos
' Get Chuncks
done = false
while done = false
'get Main Chunk
chunkend = instrb(currpos+lenb(separator) + 1,Binary,Separator,0)
if chunkend <> 0 then
if currpos > 50 then
chunk = midb(Binary,currpos + lenb(separator),chunkend-currpos)
else
chunk = midb(Binary,currpos + 1,chunkend-currpos)
end if
currpos = chunkend
'Process Chunk
DoneChunk = false
while DoneChunk = false
' ChunkLine1 contains Form/File information (ie. FileName, FormField Name)
linepos = instrb(2,chunk,chrb(13),0)
C = midb(Chunk,2,linepos)
ChunkLine1 = StrA(C)
'Is this a file? If so, get filename
linepos2 = instrb(linepos+1,chunk,chrb(13),0)
C = midb(Chunk,linepos+1,linepos2-linepos)
ChunkLine2 = StrA(C)
' if chunkline2 contains text, it is a binary file..
if len(chunkline2) > 3 then
isFile = true
else
isFile = false
end if
if isFile = false then
'Get Form Input
linepos = instrb(linepos2+1,chunk,chrb(13),0)
C = midb(Chunk,linepos2+1,linepos-linepos2)
ChunkLine3 = StrA(C)
else
' Get Binary File
linepos = instrb(linepos2+10,chunk,Separator,0)
C = midb(Chunk,linepos2+3,linepos-linepos2-3)
ChunkLine3 = C
end if
if isFile = false then
' Store Form Fields and values into FRM Dictionary object
x = split (ChunkLine1,"name="""
formName = ""
FormName = left(x(1),len(x(1))-3)
FormValue = ""
FormValue = left(ChunkLine3,len(chunkline3))
frm.Add FormName,FormValue
else
'Store File information and Binary into FILE and FILENAME Dictionary objects
x = split (ChunkLine1,"filename="""
y = split (x(0),"name="""
FormName = ""
FormName = left(y(1),len(y(1))-3)
FormValue = ""
FormValue = left(x(1),len(x(1))-3)
formValue = right(FormValue,len(FormValue)-instrRev(FormValue,"\"
fileName.Add FormName,FormValue
file.Add FormName,ChunkLine3
end if
donechunk = true
wend
else
done = true
end if
wend
end sub
call ParseBinary
'Process Files
'save file(s) to directory
for each x in FileName.Keys
if x = "ThumbFile" then
'This image is a thumbnail image
set fs = server.CreateObject ("Scripting.FileSystemObject"
set fso = fs.CreateTextFile (FileThumbPath & "\" & FileName.Item(x),true)
For lngLoop = 2 to LenB(File.Item(x))
fso.Write Chr(AscB(MidB(File.Item(x), lngLoop, 1)))
Next
fso.close
set fso = nothing
set fs = nothing
Response.Write "Saved Image: " & filename.Item(x)
Response.Write "<p><img src=""thumbs/" & filename.Item(x) & """>"
end if
next