Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

VBS FILE UPLOAD

Status
Not open for further replies.

Gorkem

Programmer
Jun 18, 2002
259
CA
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(&quot;Scripting.Dictionary&quot;)
set File = Server.CreateObject(&quot;Scripting.Dictionary&quot;)
set Frm = server.CreateObject(&quot;Scripting.Dictionary&quot;)

FileThumbPath = Server.MapPath (&quot;thumbs&quot;)

Function bstr(UStr)
'Unicode string to Byte string conversion
Dim lngLoop
Dim strChar
UStr2Bstr = &quot;&quot;
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 = &quot;&quot;
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,&quot;name=&quot;&quot;&quot;)
formName = &quot;&quot;
FormName = left(x(1),len(x(1))-3)
FormValue = &quot;&quot;
FormValue = left(ChunkLine3,len(chunkline3))
frm.Add FormName,FormValue


else
'Store File information and Binary into FILE and FILENAME Dictionary objects
x = split (ChunkLine1,&quot;filename=&quot;&quot;&quot;)
y = split (x(0),&quot;name=&quot;&quot;&quot;)
FormName = &quot;&quot;
FormName = left(y(1),len(y(1))-3)
FormValue = &quot;&quot;
FormValue = left(x(1),len(x(1))-3)
formValue = right(FormValue,len(FormValue)-instrRev(FormValue,&quot;\&quot;))
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 = &quot;ThumbFile&quot; then
'This image is a thumbnail image
set fs = server.CreateObject (&quot;Scripting.FileSystemObject&quot;)
set fso = fs.CreateTextFile (FileThumbPath & &quot;\&quot; & 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 &quot;Saved Image: &quot; & filename.Item(x)
Response.Write &quot;<p><img src=&quot;&quot;thumbs/&quot; & filename.Item(x) & &quot;&quot;&quot;>&quot;
end if
next
 
Sorry.. forgot to add the returned properties:

FRM.ITEM(Formobject)
- This holds all the regular form entries.

FILE.ITEM(FormFileObject)
- This holds all of the file binary information.

FILENAME.ITEM(FormFileObject)
- Holds the actual file name of the uploaded file.

Cheers,

Gorkem.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top