ASP : UTF-8 파일 읽고 쓰기

ASP에서 FileSystemObject를 이용해서 UTF-8 파일을 읽으면 글자가 깨진다. ASCII나 유니코드(UCS-2) 파일이 아닌 경우는 FileSystemObject로 파일을 읽을 수 없다.

ASP에서 UTF-8 파일을 읽거나 쓸 때 다음 함수를 이용하면 유용한다.

  1. Function ReadUTF8File(sFileName)
  2. Dim Stream, TextBuffer
  3.  
  4. Set Stream = Server.CreateObject("ADODB.Stream")
  5. With Stream
  6. .Charset = "utf-8"
  7. .Type = 2 'adTypeText
  8. .Open
  9. .LoadFromFile sFileName
  10. .Position = 0
  11. ReadUTF8File = .ReadText
  12. .Close
  13. End With
  14.  
  15. Set Stream = Nothing
  16. End Function
  17.  
  18. Function WriteUTF8File(sFileName, sText)
  19. Dim Stream
  20.  
  21. Set Stream = Server.CreateObject("ADODB.Stream")
  22. With Stream
  23. .Charset = "utf-8"
  24. .Type = 2 'adTypeText
  25. .Open
  26. .WriteText sText
  27. .SaveToFile sFileName, 2 'adSaveCreateOverWrite
  28. .Close
  29. End With
  30.  
  31. Set Stream = Nothing
  32. End Function

ASP : MySQL UTF-8 로 접속하기

ASP가 UTF-8 환경이고 MySQL DB가 UTF-8 일 때 다음의 연결 문자열을 사용해서 처리가 가능하다.

Driver={MySQL ODBC 5.1 driver};Server=localhost;Port=3306;Option=3;Database=db;Uid=id; Pwd=password;charset=utf8

MySQL ODBC 3.51에서는 도저히 방법을 찾지 못했다. 또한 MySQL  ODBC 5.1.10에서는 System 오류(Specified driver could not be loaded due to system error 127)가 발생했고, MySQL ODBC 5.1.4 버전에서는 동작을 했다.

Response.CodePage

Classic ASP에서 UTF-8을 사용하기 위해서 Response.CodePage = 65001 을 했더니 “Object doesn’t support this property” 란 오류가 발생한다.

MSDN에서는 IIS 5.0에서 되어있는데 지원을 안하네. 윈도우 2000 서버라서 그런가?
해결방법을 찾는 중…..