MultiformProcessor.class.asp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. <%
  2. ' Power by Techird
  3. ' Processor Usage:
  4. ' Set p = new MultiformProcessor
  5. ' Set formValues = p.Process()
  6. ' filename = formValues.Item("filename")
  7. ' Set stream = formValues.Item("file1") // the name of the file input
  8. ' stream.SaveToFile "upload/" & filename
  9. ' stream.Close
  10. Class MultiformProcessor
  11. Private adTypeBinary
  12. Private adTypeText
  13. Private adModeReadWrite
  14. Private binCtLf
  15. Private binCtLf2
  16. private Sub Class_Initialize()
  17. adTypeBinary = 1
  18. adTypeText = 2
  19. adModeReadWrite = 3
  20. binCtLf = ChrB(13) & ChrB(10)
  21. binCtLf2 = binCtLf & binCtLf
  22. End Sub
  23. Private Function OpenStream( optype )
  24. Set stream = Server.CreateObject("ADODB.Stream")
  25. stream.Type = optype
  26. stream.Mode = adModeReadWrite
  27. stream.Open
  28. Set OpenStream = stream
  29. End Function
  30. Private Function CopyStreamPart( stream, pBgn, pEnd )
  31. Dim iStream, oStream
  32. Set iStream = stream
  33. Set oStream = OpenStream( adTypeBinary )
  34. iStream.Position = pBgn
  35. iStream.CopyTo oStream, pEnd - pBgn
  36. Set CopyStreamPart = oStream
  37. End Function
  38. Private Function GetString( stream, pBgn, pEnd )
  39. Dim iStream, oStream
  40. Set iStream = stream
  41. Set oStream = OpenStream( adTypeBinary )
  42. iStream.Position = pBgn
  43. iStream.CopyTo oStream, pEnd - pBgn
  44. oStream.Position = 0
  45. oStream.Type = adTypeText
  46. oStream.Charset = GetCharset
  47. GetString = oStream.ReadText
  48. oStream.Close
  49. End Function
  50. Private Function GetCharset()
  51. If Charset = "" Then
  52. GetCharset = "utf-8"
  53. Else
  54. GetCharset = Charset
  55. End If
  56. End Function
  57. 'See RFC 2388
  58. 'http://www.ietf.org/rfc/rfc2388.txt
  59. public Function Process()
  60. Dim formBytes, bLen, pBgn, pEnd, header, stream
  61. Dim varPtn, filePtn, formValues, key, field
  62. formBytes = Request.BinaryRead( Request.TotalBytes )
  63. Set stream = OpenStream( adTypeBinary )
  64. stream.Write formBytes
  65. Set varPtn = new RegExp
  66. varPtn.Pattern = "(\w+?)=""(.+?)"""
  67. varPtn.Global = True
  68. Set filePtn = new RegExp
  69. filePtn.Pattern = "filename="
  70. Set formValues = Server.CreateObject("Scripting.Dictionary")
  71. 'Find boundary
  72. bLen = InStrB( 1, formBytes, binCtLf ) - 1
  73. boundary = LeftB( formBytes, bLen )
  74. 'Init begin pointer to byte start
  75. pBgn = 1
  76. Do
  77. 'Find begin pointer and end pointer for block header
  78. pBgn = pBgn + bLen + LenB( binCtLf ) - 1
  79. pEnd = InStrB( pBgn, formBytes, binCtLf2 )
  80. 'If next block not found, means all blocks processed
  81. If pEnd = 0 Then
  82. Exit Do 'Load Finished
  83. End If
  84. 'Decode the headerf
  85. header = GetString( stream, pBgn, pEnd )
  86. 'Test if the block is a file block
  87. isFileBlock = filePtn.Test( header )
  88. 'Find begin pointer and end pointer for block content
  89. pBgn = pEnd + LenB(binCtLf2) - 1
  90. pEnd = InStrB(pBgn, formBytes, boundary) - LenB( binCtLf ) - 1
  91. 'Extract field values from header, which like key = "filed"
  92. Set matches = varPtn.Execute( header )
  93. For Each match In matches
  94. key = match.SubMatches(0)
  95. field = match.SubMatches(1)
  96. 'filename as a field
  97. If key = "filename" Then
  98. formValues.Add key, field
  99. 'name specified fields
  100. ElseIf key = "name" Then
  101. If isFileBlock Then
  102. 'Resolve content as stream for fileblock
  103. formValues.Add field, CopyStreamPart(stream, pBgn, pEnd)
  104. Else
  105. 'Resolve content as string for non-fileblock
  106. formValues.Add field, GetString(stream, pBgn, pEnd)
  107. End If
  108. End If
  109. Next
  110. 'Move over the begin pointer to next block
  111. pBgn = pEnd + LenB( binCtLf ) + 1
  112. Loop
  113. stream.Close
  114. Set Process = formValues
  115. End Function
  116. End Class
  117. %>