자료/ASP

Dext(v3.2.1), ABC(v4.1), TABS(v3.0)을 이용한 Upload Class

네오블루 2008. 9. 1. 20:31
완전 하진 않지만 최대한 편하게 사용할수 있도록 구성 해봤습니다.
혹시 필요 하신분들이 있지 않을까 싶어서 올려는데요.
UPload Class를 사용 하시려면
DextUpload, ABCUpload, TABS 업로드 컴퍼넌트가 설치 되어 있어야 하며
섬네일 이미지를 생성 하려면 DextUpload또는 Nanumi Image Plus 컴퍼넌트가 필요 합니다.

 

Nanumi Image Plus 컴퍼넌트 는 nanumi.net에서 받으실수 있습니다

 

DextUpload는 3.2.1상태에서 작성한것입니다. 아마도 조금더 낮은 버전에서도 무리 없이 될것이라 생각됩니다.
<%
Class clsFileUpload
Private objUpload, objFSO

 

Private strUseUpload '업로드 객체 종류
Private strDefaultPath '기본 업로드 경로
Private intMaxUploadSize '업로드 최대 용량 설정 byte단위
Private blnFileOverWrite '업로드 파일 중 중복된 파일이 있을경우 OverWrite 여부
Private strFileTypeLimit '업로드 가능한 파일 확장자 설정(콤마[,]로 구분) ex) jpg,gif,swf ...

 

Private strSaveFileName '저장된 파일의 명
Private strSaveUploadPath '파일이 저장된 물리적 경로

 

Private strCGI '업로드 파일에 CGI파일명을 제한

 

Private arrUpFile(100) '파일경로 저장
Private intUpFileCount '업로드 파일 총 개수

 

'===================================================================
==
===
' 클래스 생성자 - 프로퍼티 및 객체 기본 설정
'===================================================================
==
===
Private Sub Class_Initialize()
strUseUpload = "DEXT"
blnOtherThumbnail = False
strDefaultPath = Server.MapPath("\")
intMaxUploadSize = 1024 * 1024 '기본 1Mbyte
strCGI = "asp,asa,aspx,php,jsp,cgi,js,css,html"

 

intUpFileCount = 0

 

Set objFSO = Server.CreateObject("Scripting.FileSystemObject")
End Sub

 

'===================================================================
==
===
' 업로드시 사용될 업로드 객체 설정
'===================================================================
==
===
Public Property Let UseUpload(strVal)
If strVal <> "" And Not IsNull(strVal) Then
strUseUpload = UCase(strVal)
End If
End Property

 

Public Property Get UseUpload()
UseUpload = strUseUpload
End Property

 

'===================================================================
==
===
' 기본경로 설정
'===================================================================
==
===
Public Property Let DefaultPath(strVal)
strDefaultPath = strVal
End Property

 

Public Property Get DefaultPath()
DefaultPath = strDefaultPath
End Property

 

'===================================================================
==
===
' 파일 업로드 최대용량 설정
'===================================================================
==
===
Public Property Let MaxUploadSize(intVal)
If intVal <> "" And Not IsNull(intVal) Then
intMaxUploadSize = intVal
End If
End Property

 

Public Property Get MaxUploadSize()
MaxUploadSize = intMaxUploadSize
End Property

 

'===================================================================
==
===
' 업로드 파일 덮어 쓰기 여부(True -> 가능)
'===================================================================
==
===
Public Property Let OverWrite(blnVal)
If blnVal <> "" And Not IsNull(blnVal) Then
blnFileOverWrite = blnVal
End If
End Property

 

Public Property Get OverWrite()
OverWrite = blnFileOverWrite
End Property

 

'===================================================================
==
===
' 업로드 파일의 확장자 제한(설정 없을 경우 CGI파일만 제한)
'===================================================================
==
===
Public Property Let FileTypeLimit(strVal)
If blnVal <> "" And Not IsNull(strVal) Then
FileTypeLimit = strVal
End If
End Property

 

Public Property Get FileTypeLimit()
FileTypeLimit = strFileTypeLimit
End Property

 

'===================================================================
==
===
' 업로드파일 저장 후 파일명
'===================================================================
==
===
Public Property Get SaveFileName()
SaveFileName = strSaveFileName
End Property

 

'===================================================================
==
===
' 업로드파일 저장 후 파일명
'===================================================================
==
===
Public Property Get SaveUploadPath()
SaveUploadPath = strSaveUploadPath
End Property

 

'===================================================================
==
===
' 설정된 대상으로 Upload 객체 생성
'===================================================================
==
===
Public Sub setUpload()
Select Case UCase(strUseUpload)
Case "DEXT"
Set objUpload = Server.CreateObject("DEXT.FileUpload")

 

objUpload.AutoMakeFolder = True '업로드 폴더가 없을 경우 자동 생성
objUpload.MaxFileLen = intMaxUploadSize
Case "ABC"
Set objUpload = Server.CreateObject("ABCUpload4.XForm")

 

objUpload.AbsolutePath = True '저장경로를 절대 경로로 설정
objUpload.MaxUploadSize = intMaxUploadSize
objUpload.OverWrite = blnFileOverWrite
Case "TABS"
Set objUpload = Server.CreateObject("TABS.Upload")
End Select

 

If strUseUpload = "DEXT" Or strUseUpload = "TABS" Then
If strDefaultPath <> "" Then
If strUseUpload = "DEXT" Then
objUpload.DefaultPath = strDefaultPath
ElseIf strUseUpload = "TABS" Then
objUpload.Start strDefaultPath
objUpload.MaxBytesToAbort = intMaxUploadSize
End If
End If
End If
End Sub

 

'===================================================================
==
===
' Form값 Request
'===================================================================
==
===
Public Function RequestForm(strVal)
If Not IsObject(objUpload) Then
setUpload()
End IF

 

If strUseUpload <> "ABC" Then
RequestForm = Replace(objUpload.Form(strVal), "'", "''")
Else
RequestForm = Replace(objUpload.Item(strVal), "'", "''")
End If
End Function

 

'===================================================================
==
===
' 지정된 경로의 폴더가 있는지 검수 후 없을 경우 폴더를 생성한다
'===================================================================
==
===
Private Function UploadPathCheck(strPath)
Dim Path, Flag : Flag = False
Dim ReturnFileName

 

Path = PathTypeCheck(strPath)

 

'업로드 할 폴더가 있는지 검수하여 없는 경우 폴더 생성
If Not objFSO.FolderExists(Path) Then ' And Flag = False
objFSO.CreateFolder(Path)
End If

 

UploadPathCheck = Path
End Function

 

'===================================================================
==
===
' 경로 형식을 통일 하기 위한 처리
'===================================================================
==
===
Private Function PathTypeCheck(strPath)
Dim Path

 

If strPath = "" Or IsNull(strPath) Then
Path = strDefaultPath

 

If Right(Path, 1) <> "\" Then
Path = Path &"\"
End If
Else
Path = strPath

 

If Right(Path, 1) <> "\" Then
Path = Path &"\"
End If
End If

 

PathTypeCheck = Path
End Function

 

'===================================================================
==
===
' 파일의 확장자 검수
'===================================================================
==
===
Private Sub FileExtCheck(strFileName)
Dim ExtName, arrFileName, arrExt, arrCGI, i, Chk

 

Chk = False

 

arrFileName = FileExtReturn(strFileName)
ExtName = arrFileName(1)

 

If strCGI <> "" Then
arrCGI = Split(strCGI, i)

 

For i = 0 To UBound(arrCGI)
If ExtName = arrCGI(i) Then
UploadFileAllDel()

 

Response.Write "<SCRIPT LANGUAGE=""JavaScript"">"
Response.Write "alert('CGI파일은 업로드 할수 없습니다!');"
Response.Write "history.back();"
Response.Write "</SCRIPT>"
Response.End
End If
Next
End If

 

'업로드가 가능한 파일 검수
If strFileTypeLimit <> "" Then
arrExt = Split(strFileTypeLimit, ",")

 

For i = 0 To UBound(arrExt)
If arrExt(i) = ExtName Then
Chk = True
Exit For
End If
Next
End If

 

If Chk = False Then
UploadFileAllDel()

 

Response.Write "<SCRIPT LANGUAGE=""JavaScript"">"
Response.Write "alert('업로드 할수없는 형식입니다');"
Response.Write "history.back();"
Response.Write "</SCRIPT>"
Response.End
End If
End Sub

 

'===================================================================
==
===
' 업로드된 파일의 확장자명을 반환
'===================================================================
==
===
Private Function FileExtReturn(strFileName)
Dim arrName, ExtName, i
Dim arrFileData(1) '(0) = 파일명, (1) = 확장자 명

 

arrName = Split(strFileName, ".")

 

For i = 0 To UBound(arrName) - 1
If i = 0 Then
arrFileData(0) = arrName(i)
Else
arrFileData(0) = arrFileData(0) &"."& arrName(i)
End If
Next

 

arrFileData(1) = arrName(UBound(arrName))

 

FileExtReturn = arrFileData
End Function

 

'===================================================================
==
===
' 파일명을 숫자를 조합하여 랜덤 생성
'===================================================================
==
===
Private Function RndFileNameMake(strFileName, strFileFrontName, strPath)
Dim arrFileName

 

arrFileName = FileExtReturn(strFileName) '확장자명 축출

 

ExtName = arrFileName(1) '파일 확장자명

 

'랜덤 파일명 생성
Randomize
RndFileName = strFileFrontName &"_"& CStr(Int((99999999 * Rnd) + 1)) &"."& ExtName

 

'파일 명이 중복되지 않을때까지 파일명을 변경
Do While objFSO.FileExists(strPath & RndNum)
If objFSO.FileExists(strPath & RndFileName) Then
RndFileName = strFileFrontName &"_"& CStr(Int((99999999 * Rnd) + 1)) &"."& ExtName
End If
Loop

 

RndFileNameMake = RndFileName
End Function

 

'===================================================================
==
===
' 업로드된 파일용량 리턴
'===================================================================
==
===
Public Function UploadFileSize(strFormName)
If Not IsObject(objUpload) Then
setUpload()
End IF

 

If strUseUpload = "DEXT" Then
UploadFileSize = objUpload.Form(strFormName).FileLen
ElseIf strUseUpload = "ABC" Then
UploadFileSize = objUpload.Item(strFormName).Length
ElseIf strUseUpload = "TABS" Then
UploadFileSize = objUpload.Form(strFormName).FileSize
End If
End Function

 

'===================================================================
==
===
' 업로드 파일 사이즈 검수(업로드 용량을 초과할 경우 False반환)
'===================================================================
==
===
Public Function FileSizeCheck(strFormName)
If intMaxUploadSize < UploadFileSize(strFormName) Then
FileSizeCheck = False
Else
FileSizeCheck = True
End If
End Function

 

'===================================================================
==
===
' 업로드 파일 저장
' strFormName -> 업로드된 파일 폼 명(필수)
' strPath -> 저장 경로(설정하지 않을 경우 기본경로)
' strFileName -> 저장할 파일명(설정 하지 않을 경우 업로드된 파일명 기본)
'===================================================================
==
===
Public Sub Save(strFormName, strPath, strFileName)
Dim Path

 

Path = UploadPathCheck(strPath)

 

If Not IsObject(objUpload) Then
setUpload()
End IF

 

'=================================================================
===
====
' Dext와 Tabs는 OverWrite이 False설정 되어 있고 중복된 파일이 있을 경우
' 겹치지 않는 파일명으로 자동 저장이 됨.
'=================================================================
===
====
If strUseUpload = "DEXT" Then
If strFileName <> "" Then
objUpload(strFormName).SaveAs Path & strFileName, blnFileOverWrite
Else
objUpload(strFormName).Save , blnFileOverWrite
End If

 

strSaveFileName = objUpload.Form(strFormName).LastSavedFileName

 

ElseIf strUseUpload = "ABC" Then
'ABC업드의 경우 한글 파일명이 제대로 저장되지 않는 경우를 배제 하기 위해 랜덤 파일명 생성
If strFileName <> "" Then
objUpload.Item(strFormName)(1).Save Path & strFileName

 

strSaveFileName = strFileName
Else
strSaveFileName = RndFileNameMake(objUpload.Item(strFormName)(1).FileName, "upload",
Path)

 

objUpload.Item(strFormName)(1).Save Path & strSaveFileName
End If

 

arrUpFile(intUpFileCount) = Path & strSaveFileName
intUpFileCount = intUpFileCount + 1

 

ElseIf strUseUpload = "TABS" Then
If strFileName <> "" Then
objUpload.Form(strFormName).SaveAs Path & strFileName, blnFileOverWrite
Else
objUpload.Form(strFormName).Save Path, blnFileOverWrite
End If

 

strSaveFileName = objUpload.Form(strFormName).ShortSaveName
End If

 

strSaveUploadPath = Path
End Sub

 

'===================================================================
==
===
' 파일명을 랜덤으로 생성 시켜 업로드 저장
'===================================================================
==
===
Public Sub RndFileNameSave(strFormName, strFileFrontName, strPath)
Dim Path
Dim RndFileName, getFileName, ExtName, arrName

 

If Not IsObject(objUpload) Then
setUpload()
End IF

 

If FileFrontName = "" Or Not IsNull(FileFrontName) Then
strFileFrontName = "upload"
End If

 

If strUseUpload = "DEXT" Or strUseUpload = "TABS" Then
getFileName = objUpload.Form(strFormName).FileName
ElseIf strUseUpload = "ABC" Then
getFileName = objUpload.Item(strFormName)(1).FileName
End If

 

Path = PathTypeCheck(strPath)

 

'랜덤 파일명 생성
RndFileName = RndFileNameMake(getFileName, strFileFrontName, strPath)

 

Save strFormName, strPath, RndFileName
End Sub

 

'===================================================================
==
===
' 업로드된 파일을 호출하여 섬네일 이미지를 생성한다
' DEXT의 경우는 ImageProc객체를 사용하여 섬네일을 생성하며
' ABC, TABS는 nanumi image plus 객체를 사용하여 생성한다
' nanumi image plus가 설치 되어 있지 않을 경우 에러 발생
' 섬네일 파일의 이미지는 원본이미지와 같은 명으로 다른 폴더에 저장
' strFullFilePath -> 파일명을 포함한 전체 물리적 경로
' intWidth -> 섬네일 이미지의 넓이
' intHeight -> 섬네일 이미지의 높이
' strSavePath -> 섬네일 이미지가 저장되는 경로(원본이지미와 다른경로)
' strFormName -> 업로드 이미지 Form 소스(DextUpload만 사용)
'===================================================================
==
===
Public Sub ImgThumbCreate(strFullFilePath, intWidth, intHeight, strSavePath, strFormName)
Dim i
Dim objImg, SavePath
Dim arrPath, UpPath, FileName, arrFileName, FileNameWithoutExt, ExtName

 

If Not IsObject(objUpload) Then
setUpload()
End IF

 

'경로가 설정 되어 있지 않는 경우 업로 원본 파일에 thumb폴더를 생성
If strSavePath = "" Or IsNull(strSavePath) Then
arrPath = Split(strFullFilePath, "\") '물리적경로를 \기준으로 분리

 

'파일명을 제외한 경로 저장
For i = 0 To UBound(arrPath) - 1
If i = 0 Then
UpPath = arrPath(i)
Else
UpPath = UpPath &"\"& arrPath(i)
End If
Next

 

FileName = arrPath(UBound(arrPath))

 

UpPath = UploadPathCheck(UpPath &"\thumb\")
Else
UpPath = UploadPathCheck(PathTypeCheck(strSavePath))
End If

 

'파일명을 확장자를 분리하여 저장
arrFileName = FileExtReturn(FileName)

 

FileNameWithoutExt = arrFileName(0) '파일명(확장자 제외)
ExtName = arrFileName(1) '확장자 명

 

'DextUpload를 이용한 섬네일 생성
If strUseUpload = "DEXT" Then
Set objImg = Server.CreateObject("DEXT.ImageProc")

 

If objImg.SetSourceFile(strFullFilePath) Then
If LCase(ExtName) = "jpg" Or LCase(ExtName) = "jpeg" then
objImg.Quality = 100
End If

 

objImg.SaveAsThumbnail UpPath & FileName, intWidth, intHeight, blnFileOverWrite
End If

 

'Nanumi Image Plus를 이용한 섬네일 생성
Else
Set objImg = Server.CreateObject("Nanumi.ImagePlus")

 

objImg.OpenImageFile strFullFilePath
objImg.ImageFormat = ExtName

 

If LCase(ExtName) = "jpg" Or LCase(ExtName) = "jpeg" Then
objImg.Quality = 100
End If

 

objImg.KeepAspect = False '이미지 사이즈를 강제로 조절
objImg.ChangeSize intWidth, intHeight
objImg.SaveFile UpPath & FileName

 

objImg.Dispose()
End If

 

Set objImg = Nothing

 

arrUpFile(intUpFileCount) = UpPath & FileName
intUpFileCount = intUpFileCount + 1

 

strSaveFileName = FileName
strSaveUploadPath = UpPath
End Sub

 

'===================================================================
==
===
' 업로드 된 파일을 일괄 삭제 한다
'===================================================================
==
===
Public Sub UploadFileAllDelete()
Dim i

 

If Not IsObject(objUpload) Then
setUpload()
End IF

 

If strUseUpload = "DEXT" Then
objUpload.DeleteAllSavedFiles()
ElseIf strUseUpload = "TABS" Then
objUpload.Delete()
End If

 

For i = 0 To intUpFileCount - 1
If objFSO.FileExists(arrUpFile(i)) Then
objFSO.DeleteFile(arrUpFile(i))
End If
Next
End Sub

 

'===================================================================
==
===
' 클래스 소멸자
'===================================================================
==
===
Private Sub Class_Terminate()
If IsObject(objFSO) Then
Set objFSO = Nothing
End If

 

If IsObject(objImg) Then
Set objImg = Nothing
End If

 

If IsObject(objUpload) Then
Set objUpload = Nothing
End If
End Sub
End Class
%>

 

<%
'사용 방법

 

ON ERROR RESUME NEXT

 

'class 인스턴트 생성
Set clsUpload = new clsFileUpload

 

With clsUpload
'기본 업로드 객체 설정
.UseUpload = "dext"
'기본 업로드 폴더 설정(절대 경로)
.DefaultPath = Server.MapPath("\") &"\upload\"
'업로드 최대 용량 설정
.MaxUploadSize = 1024 * 1024 * 2 '2Mbyte
'덮어쓰기 여부 (True : 가능, False : 불가)
.OverWrite = false
'업로드 가능한 확장자 설정
'설정하지 않을 경우 CGI파일을 제외한 모든 파일 업로드 가능
.FileTypeLimit = "jpg,jpeg,gif,bmp"

 

'FileName Form에서 넘어온 Value값을 출력한다
Response.Write .RequestForm("FileName")
'file1로 넘어온 업로드 파일의 용량을 출력한다
Response.Write .UploadFileSize("file1")

 

'업로드된 파일과 업로드 가능한 최대 용량을 비교
If .FileSizeCheck("file1") = False Then
'업로드된 파일의 용량이 최대 용량보다 큰 경우 처리
End If

 

'업로드된 파일 저장
.Save "file1", "", ""

 

'파일이 저장된 경로를 출력
Response.Write .SaveUploadPath
'파일이 저장된 명을 출력
Response.Write .SaveFileName

 

'업로드된 파일을 랜덤으로 파일명을 생성하여 저장 ex) test_103123.jpg ...
.RndFileNameSave "file2", "test", .DefaultPath &"test"

 

'저장된 파일을 이용하여 섬네일 이미지 생성 (Save나 RndFileNameSave 메서드 선 실행 필요)
.ImgThumbCreate .SaveUploadPath & .SaveFileName, 50, 50, "", ""

 

'에러가 있을 경우
If Err.Number <> 0 Then
'업로드된 모든 파일을 삭제 한다
.UploadFileAllDelete()
End If
End With

 

Set clsUpload = Nothing
%>