<%
class multipart_form
private dict
private sub Class_Initialize
set dict = Server.CreateObject("Scripting.Dictionary")
parse_form_data()
parse_query_string()
end sub
private sub parse_form_data()
if Request.TotalBytes > 0 then
dim form_content_type
form_content_type = Request.ServerVariables("HTTP_CONTENT_TYPE")
if Split(form_content_type, ";")(0) = "multipart/form-data" then
dim bin_data
bin_data = Request.BinaryRead(Request.TotalBytes)
dim bin_separator
bin_separator = MidB(bin_data, 1, InstrB(1, bin_data, ChrB(13))-1)
dim cur_pos
cur_pos = 1
dim sep_length
sep_length = LenB(bin_separator)
dim next_pos
next_pos = sep_length
dim item_length
dim item_value
While next_pos > 0
dim do_edit
do_edit = false
next_pos = InStrB(cur_pos, bin_data, bin_separator)
item_length = next_pos-cur_pos
if item_length > 1 then
item_value = MidB(bin_data, cur_pos, item_length)
dim prop_beg
dim prop_end
dim name
dim value
prop_beg = 1 + InStrB(1, item_value, ChrB(34))
prop_end = InStrB(prop_beg+0*1, item_value, ChrB(34))
dim sub_dict
set sub_dict = Server.CreateObject("Scripting.Dictionary")
name = bin2text(MidB(item_value, prop_beg, prop_end-prop_beg))
sub_dict.add "name", name
if InStrB(1, item_value, text2bin("Content-Type")) then
if not dict.exists(name) then
dim filename
dim content_type
dim file_length
prop_beg = 1 + InStrB(prop_end+1, item_value, ChrB(34))
prop_end = InStrB(prop_beg, item_value, ChrB(34))
filename = bin2text(MidB(item_value, prop_beg, prop_end-prop_beg))
sub_dict.add "filename", filename
prop_beg = 13 + 1 + InStrB(prop_end+1, item_value, text2bin("Content-Type:"))
prop_end = InStrB(prop_beg, item_value, ChrB(13))
content_type = bin2text(MidB(item_value, prop_beg, prop_end-prop_beg))
sub_dict.add "content_type", content_type
prop_beg = prop_end + 4
prop_end = item_length
file_length = prop_end-prop_beg-1
value = MidB(item_value, prop_beg, file_length)
sub_dict.add "file_length", file_length
end if
else
prop_beg = InStrB(prop_beg, item_value, ChrB(13)) + 4
value = bin2text(MidB(item_value, prop_beg, item_length-prop_beg-1))
if dict.exists(name) then
value = dict(name).Item("value") & "," & value
do_edit = true
end if
end if
sub_dict.add "value", value
if do_edit = true then
dict.remove name
end if
dict.add name, sub_dict
end if
cur_pos = next_pos + sep_length
Wend
else
show_error("Wrong Form Content type: must be 'multipart/form-data'!")
end if
end if
end sub
private sub parse_query_string()
dim i
dim sub_dict
dim query_parts
query_parts = Split(Request.ServerVariables("QUERY_STRING"), "&")
For i = 0 to UBound(query_parts)
dim var_parts
var_parts = Split(query_parts(i), "=")
if var_parts(0) <> "" and not dict.exists(var_parts(0)) then
dim var_val
set sub_dict = Server.CreateObject("Scripting.Dictionary")
if ubound(var_parts) > 0 then
var_val = var_parts(1)
else
var_val = ""
end if
sub_dict.add "value", var_val
dict.add var_parts(0), sub_dict
end if
Next
end sub
private function bin2text(bin_data)
dim i
For i = 1 to LenB(bin_data)
bin2text = bin2text & chr(AscB(MidB(bin_data,i,1)))
Next
end function
private function text2bin(text_data)
dim i
For i = 1 to Len(text_data)
text2bin = text2bin & chrB(AscB(Mid(text_data,i,1)))
Next
end function
private sub show_error(str)
Response.Write str
end sub
public function get_value(name)
if dict.exists(name) then
get_value = dict(name).Item("value")
else
get_value = ""
end if
end function
public function get_file_path(name)
if dict.exists(name) then
get_file_path = dict(name).Item("filename")
else
get_file_path = ""
end if
end function
public function get_file_dir(name)
if dict.exists(name) then
dim pos
dim filename
filename = dict(name).Item("filename")
if filename = "" then
get_file_dir = ""
end if
pos = InStrRev(filename, "\")
get_file_dir = Mid(filename, 1, pos)
else
get_file_dir = ""
end if
end function
public function get_file_name(name)
if dict.exists(name) then
dim pos
dim filename
filename = dict(name).Item("filename")
if filename = "" then
get_file_name = ""
end if
pos = InStrRev(filename, "\")
get_file_name = Mid(filename, pos+1)
else
get_file_name = ""
end if
end function
public function get_content_type(name)
if dict.exists(name) then
get_content_type = dict(name).Item("content_type")
else
get_content_type = ""
end if
end function
public function get_file_length(name)
if dict.exists(name) then
get_file_length = dict(name).Item("file_length")
else
get_file_length = 0
end if
end function
public function save_file(name, path)
if dict.exists(name) and get_file_length(name)>0 then
dim fso
set fso = Server.CreateObject("Scripting.FileSystemObject")
dim fd
' set fd = fso.CreateTextFile(Server.MapPath(path))
set fd = fso.CreateTextFile(path)
dim file_data
file_data = bin2text(get_value(name))
fd.Write file_data
fd.Close
save_file = True
else
save_file = False
end if
end function
public function get_array_of_names(name)
dim i
dim value
value = ""
dim keys
keys = dict.keys
for i=0 to dict.count-1
if instr(1, keys(i), name & "[") = 1 then
if not value = "" then
value = value & ","
end if
value = value & keys(i)
end if
next
get_array_of_names = Split(value, ",")
end function
end class
%>