%
class FileUploader
public Files
private mcolFormElem
private sub Class_Initialize()
set Files = Server.CreateObject("Scripting.Dictionary")
set mcolFormElem = Server.CreateObject("Scripting.Dictionary")
end sub
private sub Class_Terminate()
If IsObject(Files) Then
Files.RemoveAll()
Set Files = Nothing
End If
If IsObject(mcolFormElem) Then
mcolFormElem.RemoveAll()
Set mcolFormElem = Nothing
End If
end sub
public property get Form(sIndex)
Form = ""
If mcolFormElem.Exists(LCase(sIndex)) Then Form = mcolFormElem.Item(LCase(sIndex))
end property
public default sub Upload()
dim biData, sInputName
dim nPosBegin, nPosEnd, nPos, vDataBounds, nDataBoundPos
dim nPosFile, nPosBound
biData = Request.BinaryRead(Request.TotalBytes)
nPosBegin = 1
nPosEnd = InstrB(nPosBegin, biData, CByteString(Chr(13)))
if (nPosEnd-nPosBegin) <= 0 then exit sub
vDataBounds = MidB(biData, nPosBegin, nPosEnd-nPosBegin)
nDataBoundPos = InstrB(1, biData, vDataBounds)
do Until nDataBoundPos = InstrB(biData, vDataBounds & CByteString("--"))
nPos = InstrB(nDataBoundPos, biData, CByteString("Content-Disposition"))
nPos = InstrB(nPos, biData, CByteString("name="))
nPosBegin = nPos + 6
nPosEnd = InstrB(nPosBegin, biData, CByteString(Chr(34)))
sInputName = CWideString(MidB(biData, nPosBegin, nPosEnd-nPosBegin))
nPosFile = InstrB(nDataBoundPos, biData, CByteString("filename="))
nPosBound = InstrB(nPosEnd, biData, vDataBounds)
if nPosFile <> 0 And nPosFile < nPosBound then
Dim oUploadFile, sFileName
Set oUploadFile = New UploadedFile
nPosBegin = nPosFile + 10
nPosEnd = InstrB(nPosBegin, biData, CByteString(Chr(34)))
sFileName = CWideString(MidB(biData, nPosBegin, nPosEnd-nPosBegin))
if oUploadFile.FileName = "" or isNull(oUploadFile.FileName) then
''' read the name of the file
oUploadFile.FileName = Right(sFileName, Len(sFileName)-InStrRev(sFileName, "\"))
end if
nPos = InstrB(nPosEnd, biData, CByteString("Content-Type:"))
nPosBegin = nPos + 14
nPosEnd = InstrB(nPosBegin, biData, CByteString(Chr(13)))
oUploadFile.ContentType = CWideString(MidB(biData, nPosBegin, nPosEnd-nPosBegin))
nPosBegin = nPosEnd+4
nPosEnd = InstrB(nPosBegin, biData, vDataBounds) - 2
oUploadFile.FileData = MidB(biData, nPosBegin, nPosEnd-nPosBegin)
If oUploadFile.FileSize > 0 Then Files.Add LCase(sInputName), oUploadFile
else
nPos = InstrB(nPos, biData, CByteString(Chr(13)))
nPosBegin = nPos + 4
nPosEnd = InstrB(nPosBegin, biData, vDataBounds) - 2
If Not mcolFormElem.Exists(LCase(sInputName)) Then mcolFormElem.Add LCase(sInputName), CWideString(MidB(biData, nPosBegin, nPosEnd-nPosBegin))
end if
nDataBoundPos = InstrB(nDataBoundPos + LenB(vDataBounds), biData, vDataBounds)
loop
end sub
'string to byte string conversion
private function CByteString(sString)
Dim nIndex
For nIndex = 1 to Len(sString)
CByteString = CByteString & ChrB(AscB(Mid(sString,nIndex,1)))
Next
end function
'byte string to string conversion
private function CWideString(bsString)
Dim nIndex
CWideString =""
For nIndex = 1 to LenB(bsString)
CWideString = CWideString & Chr(AscB(MidB(bsString,nIndex,1)))
Next
end function
end class
class UploadedFile
public ContentType
public FileName
public FileData
public b_error
public property get FileSize()
FileSize = LenB(FileData)
end property
public sub SaveToDisk(sPath)
dim oFS, oFile
dim nIndex
dim ScriptTimeOutValue
ScriptTimeOutValue=Server.ScriptTimeOut
Server.scriptTimeOut=3600
if sPath = "" OR FileName = "" then exit sub
if Mid(sPath, Len(sPath)) <> "\" then sPath = sPath & "\"
set oFS = server.CreateObject("Scripting.FileSystemObject")
' :: create folder if it does not exist
on error resume next
if not oFS.FolderExists(sPath) then oFS.CreateFolder(sPath)
if err.number <> 0 then
b_error = true
exit sub
end if
set oFile = oFS.CreateTextFile(sPath & FileName, True)
if err.number <> 0 then
b_error = true
exit sub
end if
on error goto 0
for nIndex = 1 to LenB(FileData)
oFile.Write Chr(AscB(MidB(FileData,nIndex,1)))
next
oFile.Close
set oFS = nothing
Server.ScriptTimeout=ScriptTimeOutValue
end sub
public sub SaveToDatabase(ByRef oField)
if LenB(FileData) = 0 then exit sub
if IsObject(oField) then
oField.AppendChunk FileData
End If
end sub
end class
function upload_files(target_dir, filename)
''' create the FileUploader
dim uploader, file
set uploader = new FileUploader
' this starts the upload process
uploader.Upload()
' check if any files were uploaded
if uploader.files.count = 0 then
upload_files = 0
else
' loop through the uploaded files
for each file in Uploader.files.items
' save the file
'file.FileName = filename
file.SaveToDisk target_dir
if file.b_error then exit function
if filename = "" then last_filename = Right(file.FileName, Len(file.FileName)-InStrRev(file.FileName, "\"))
' output the file details to the browser
'response.Write target_dir
'response.Write file.FileName & "
"
'response.Write "Size: " & file.FileSize & " bytes
"
'response.Write "Type: " & file.ContentType & "
"
next
upload_files = uploader.files.count
end if
end function
function delete_file(filename)
set oFS = server.CreateObject("Scripting.FileSystemObject")
if oFS.FileExists(filename) then
oFS.DeleteFile(filename)
delete_file = 1
else
delete_file = 0
end if
set oFS = NOTHING
end function
%>