fileUploadClass.asp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  1. <%
  2. '----------------------------------------------------------------------
  3. ' CHEditor ASP File Upload Class
  4. '
  5. ' 1. attach 디렉토리는 웹에서 파일을 쓰고, 읽을 수 있도록 퍼미션을 조정하여 주십시오.
  6. ' 2. 이미지 업로드는 "\program Files\Common Files\System\ado\msado15.dll" 를 이용합니다.
  7. ' 만약 위 모듈이 레지스트리에 등록되어 있지 않을 경우, regsvr32 명령을 이용하여 등록하여 주십시오.
  8. '
  9. ' 보기: regsvr32 "c:\program files\common files\system\ado\msado15.dll"
  10. ' ASP의 경우 기본 업로드 크기가 200K로 기본 설정이 되어있습니다.
  11. ' 아래 과정을 통해 원하는 파일 크기로 재설정할 수 있습니다.
  12. ' 1. IIS 서비스를 멈춤니다.
  13. ' 2. 'c:\windows\system32\inetsrv\Metabase.XML' 파일을 간단한 에디터(노트패드와 같은) 열어
  14. ' AspMaxRequestEntityAllowed="204800"으로 설정되어 있는 값을 원하는는 크기로 수정합니다.
  15. ' 3. IIS를 시작합니다.
  16. '----------------------------------------------------------------------
  17. const DEFAULT_CHUNK_SIZE = 200000
  18. const adModeReadWrite = 3
  19. const adTypeBinary = 1
  20. const adTypeText = 2
  21. const adSaveCreateOverWrite = 2
  22. Class fileUploadClass
  23. Public UploadedFiles
  24. Public FormElements
  25. Private VarArrayBinRequest
  26. Private StreamRequest
  27. Private uploadedYet
  28. Private internalChunkSize
  29. Private SaveDirectory
  30. Private Sub Class_Initialize()
  31. Set UploadedFiles = Server.CreateObject("Scripting.Dictionary")
  32. Set FormElements = Server.CreateObject("Scripting.Dictionary")
  33. Set StreamRequest = Server.CreateObject("ADODB.Stream")
  34. With StreamRequest
  35. .Open
  36. .Type = adTypeText
  37. End With
  38. uploadedYet = false
  39. internalChunkSize = DEFAULT_CHUNK_SIZE
  40. End Sub
  41. Private Sub Class_Terminate()
  42. If IsObject(UploadedFiles) Then
  43. UploadedFiles.RemoveAll()
  44. Set UploadedFiles = Nothing
  45. End If
  46. If IsObject(FormElements) Then
  47. FormElements.RemoveAll()
  48. Set FormElements = Nothing
  49. End If
  50. StreamRequest.Close
  51. Set StreamRequest = Nothing
  52. End Sub
  53. Public Property Get Form(sIndex)
  54. Form = ""
  55. If FormElements.Exists(LCase(sIndex)) Then Form = FormElements.Item(LCase(sIndex))
  56. End Property
  57. Public Property Get Files()
  58. Files = UploadedFiles.Items
  59. End Property
  60. Public Property Get Exists(sIndex)
  61. Exists = false
  62. If FormElements.Exists(LCase(sIndex)) Then Exists = true
  63. End Property
  64. Public Property Get FileExists(sIndex)
  65. FileExists = false
  66. if UploadedFiles.Exists(LCase(sIndex)) then FileExists = true
  67. End Property
  68. Public Property Get chunkSize()
  69. chunkSize = internalChunkSize
  70. End Property
  71. Public Property Let chunkSize(sz)
  72. internalChunkSize = sz
  73. End Property
  74. Public Sub Save(path)
  75. Dim streamFile, fileItem, filePath
  76. If Right(path, 1) <> "\" then path = path & "\"
  77. SaveDirectory = path
  78. If not uploadedYet then Upload
  79. For Each fileItem In UploadedFiles.Items
  80. filePath = path & fileItem.FileName
  81. Set streamFile = Server.CreateObject("ADODB.Stream")
  82. streamFile.Type = adTypeBinary
  83. streamFile.Open
  84. StreamRequest.Position=fileItem.Start
  85. StreamRequest.CopyTo streamFile, fileItem.Length
  86. streamFile.SaveToFile filePath, adSaveCreateOverWrite
  87. streamFile.close
  88. Set streamFile = Nothing
  89. fileItem.Path = filePath
  90. Next
  91. End Sub
  92. Public Function SaveBinRequest(path)
  93. StreamRequest.SaveToFile path & "\debugStream.bin", 2
  94. End Function
  95. Public Sub Upload()
  96. Dim nCurPos, nDataBoundPos, nLastSepPos
  97. Dim nPosFile, nPosBound
  98. Dim sFieldName, osPathSep, auxStr
  99. Dim readBytes, readLoop, tmpBinRequest
  100. 'RFC1867 토큰
  101. Dim vDataSep
  102. Dim tNewLine, tDoubleQuotes, tTerm, tFilename, tName, tContentDisp, tContentType
  103. tNewLine = String2Byte(Chr(13))
  104. tDoubleQuotes = String2Byte(Chr(34))
  105. tTerm = String2Byte("--")
  106. tFilename = String2Byte("filename=""")
  107. tName = String2Byte("name=""")
  108. tContentDisp = String2Byte("Content-Disposition")
  109. tContentType = String2Byte("Content-Type:")
  110. uploadedYet = true
  111. on error resume next
  112. readBytes = internalChunkSize
  113. VarArrayBinRequest = Request.BinaryRead(readBytes)
  114. VarArrayBinRequest = midb(VarArrayBinRequest, 1, lenb(VarArrayBinRequest))
  115. Do Until readBytes < 1
  116. tmpBinRequest = Request.BinaryRead(readBytes)
  117. if readBytes > 0 then
  118. VarArrayBinRequest = VarArrayBinRequest & midb(tmpBinRequest, 1, lenb(tmpBinRequest))
  119. end if
  120. Loop
  121. StreamRequest.WriteText(VarArrayBinRequest)
  122. StreamRequest.Flush()
  123. if Err.Number <> 0 then
  124. response.write "<br><br><B>시스템 오류 목록:</B><p>"
  125. response.write Err.Description & "<p>"
  126. response.write "IIS MetaBase의 AspMaxRequestEntityAllowed 값을 설정하여 주십시오.<p>"
  127. Exit Sub
  128. end if
  129. on error goto 0 '리셋 오류 핸들링
  130. nCurPos = FindToken(tNewLine,1)
  131. If nCurPos <= 1 Then Exit Sub
  132. vDataSep = MidB(VarArrayBinRequest, 1, nCurPos-1)
  133. '시작
  134. nDataBoundPos = 1
  135. '끝 라인 시작
  136. nLastSepPos = FindToken(vDataSep & tTerm, 1)
  137. Do Until nDataBoundPos = nLastSepPos
  138. nCurPos = SkipToken(tContentDisp, nDataBoundPos)
  139. nCurPos = SkipToken(tName, nCurPos)
  140. sFieldName = ExtractField(tDoubleQuotes, nCurPos)
  141. nPosFile = FindToken(tFilename, nCurPos)
  142. nPosBound = FindToken(vDataSep, nCurPos)
  143. If nPosFile <> 0 And nPosFile < nPosBound Then
  144. Dim oUploadFile
  145. Set oUploadFile = New UploadedFile
  146. nCurPos = SkipToken(tFilename, nCurPos)
  147. auxStr = ExtractField(tDoubleQuotes, nCurPos)
  148. osPathSep = "\"
  149. If InStr(auxStr, osPathSep) = 0 then osPathSep = "/"
  150. oUploadFile.FileName = Right(auxStr, Len(auxStr)-InStrRev(auxStr, osPathSep))
  151. If (Len(oUploadFile.FileName) > 0) then
  152. nCurPos = SkipToken(tContentType, nCurPos)
  153. auxStr = ExtractField(tNewLine, nCurPos)
  154. oUploadFile.ContentType = Right(auxStr, Len(auxStr)-InStrRev(auxStr, " "))
  155. nCurPos = FindToken(tNewLine, nCurPos) + 4 '빈 라인 건너 뜀
  156. oUploadFile.Start = nCurPos+1
  157. oUploadFile.Length = FindToken(vDataSep, nCurPos) - 2 - nCurPos
  158. If oUploadFile.Length > 0 Then UploadedFiles.Add LCase(sFieldName), oUploadFile
  159. End If
  160. Else
  161. Dim nEndOfData, fieldValueUniStr
  162. nCurPos = FindToken(tNewLine, nCurPos) + 4 '빈 라인 건너 뜀
  163. nEndOfData = FindToken(vDataSep, nCurPos) - 2
  164. fieldValueuniStr = ConvertUtf8BytesToString(nCurPos, nEndOfData-nCurPos)
  165. If Not FormElements.Exists(LCase(sFieldName)) Then
  166. FormElements.Add LCase(sFieldName), fieldValueuniStr
  167. else
  168. FormElements.Item(LCase(sFieldName))= FormElements.Item(LCase(sFieldName)) & ", " & fieldValueuniStr
  169. end if
  170. End If
  171. nDataBoundPos = FindToken(vDataSep, nCurPos)
  172. Loop
  173. 'StreamRequest.WriteText(VarArrayBinRequest)
  174. End Sub
  175. Private Function SkipToken(sToken, nStart)
  176. SkipToken = InstrB(nStart, VarArrayBinRequest, sToken)
  177. If SkipToken = 0 then
  178. Response.write "처리중 오류가 발생하였습니다. IIS MetaBase의 AspMaxRequestEntityAllowed 값을 설정하여 주십시오.<p>"
  179. Response.End
  180. end if
  181. SkipToken = SkipToken + LenB(sToken)
  182. End Function
  183. Private Function FindToken(sToken, nStart)
  184. FindToken = InstrB(nStart, VarArrayBinRequest, sToken)
  185. End Function
  186. Private Function ExtractField(sToken, nStart)
  187. Dim nEnd
  188. nEnd = InstrB(nStart, VarArrayBinRequest, sToken)
  189. If nEnd = 0 then
  190. Response.write "처리중 오류가 발생하였습니다."
  191. Response.End
  192. end if
  193. ExtractField = ConvertUtf8BytesToString(nStart, nEnd-nStart)
  194. End Function
  195. Private Function String2Byte(sString)
  196. Dim i
  197. For i = 1 to Len(sString)
  198. String2Byte = String2Byte & ChrB(AscB(Mid(sString,i,1)))
  199. Next
  200. End Function
  201. Private Function ConvertUtf8BytesToString(start, length)
  202. StreamRequest.Position = 0
  203. Dim objStream
  204. Dim strTmp
  205. Set objStream = Server.CreateObject("ADODB.Stream")
  206. objStream.Charset = "utf-8"
  207. objStream.Mode = adModeReadWrite
  208. objStream.Type = adTypeBinary
  209. objStream.Open
  210. StreamRequest.Position = start+1
  211. StreamRequest.CopyTo objStream, length
  212. objStream.Flush
  213. objStream.Position = 0
  214. objStream.Type = adTypeText
  215. strTmp = objStream.ReadText
  216. objStream.Close
  217. Set objStream = Nothing
  218. ConvertUtf8BytesToString = strTmp
  219. End Function
  220. End Class
  221. Class UploadedFile
  222. Public ContentType
  223. Public Start
  224. Public Length
  225. Public Path
  226. Public origName
  227. Private nameOfFile
  228. Public Property Let FileName(fN)
  229. nameOfFile = fN
  230. nameOfFile = SubstNoReg(nameOfFile, "\", "_")
  231. nameOfFile = SubstNoReg(nameOfFile, "/", "_")
  232. nameOfFile = SubstNoReg(nameOfFile, ":", "_")
  233. nameOfFile = SubstNoReg(nameOfFile, "*", "_")
  234. nameOfFile = SubstNoReg(nameOfFile, "?", "_")
  235. nameOfFile = SubstNoReg(nameOfFile, """", "_")
  236. nameOfFile = SubstNoReg(nameOfFile, "<", "_")
  237. nameOfFile = SubstNoReg(nameOfFile, ">", "_")
  238. nameOfFile = SubstNoReg(nameOfFile, "|", "_")
  239. End Property
  240. Public Property Get FileName()
  241. FileName = nameOfFile
  242. End Property
  243. End Class
  244. Function SubstNoReg(initialStr, oldStr, newStr)
  245. Dim currentPos, oldStrPos, skip
  246. If IsNull(initialStr) Or Len(initialStr) = 0 Then
  247. SubstNoReg = ""
  248. ElseIf IsNull(oldStr) Or Len(oldStr) = 0 Then
  249. SubstNoReg = initialStr
  250. Else
  251. If IsNull(newStr) Then newStr = ""
  252. currentPos = 1
  253. oldStrPos = 0
  254. SubstNoReg = ""
  255. skip = Len(oldStr)
  256. Do While currentPos <= Len(initialStr)
  257. oldStrPos = InStr(currentPos, initialStr, oldStr)
  258. If oldStrPos = 0 Then
  259. SubstNoReg = SubstNoReg & Mid(initialStr, currentPos, Len(initialStr) - currentPos + 1)
  260. currentPos = Len(initialStr) + 1
  261. Else
  262. SubstNoReg = SubstNoReg & Mid(initialStr, currentPos, oldStrPos - currentPos) & newStr
  263. currentPos = oldStrPos + skip
  264. End If
  265. Loop
  266. End If
  267. End Function
  268. Function GetFileName(strSaveToPath, FileName)
  269. Dim Counter
  270. Dim Flag
  271. Dim strTempFileName
  272. Dim FileExt
  273. Dim NewFullPath
  274. Dim objFSO, p
  275. Set objFSO = CreateObject("Scripting.FileSystemObject")
  276. Counter = 0
  277. p = InStrRev(FileName, ".")
  278. FileExt = Mid(FileName, p+1)
  279. strTempFileName = Left(FileName, p-1)
  280. NewFullPath = strSaveToPath & "\" & FileName
  281. Flag = False
  282. Do Until Flag = True
  283. If objFSO.FileExists(NewFullPath) = False Then
  284. Flag = True
  285. GetFileName = Mid(NewFullPath, InstrRev(NewFullPath, "\") + 1)
  286. Else
  287. Counter = Counter + 1
  288. NewFullPath = strSaveToPath & "\" & strTempFileName & "_" & Counter & "." & FileExt
  289. End If
  290. Loop
  291. End Function
  292. %>