3초기억력

classic asp 에서 업로드컴포넌트 dll 파일 생성법 및 사용법 본문

플밍_ASP

classic asp 에서 업로드컴포넌트 dll 파일 생성법 및 사용법

잠수콩 2017. 11. 7. 09:48
336x280(권장), 300x250(권장), 250x250, 200x200 크기의 광고 코드만 넣을 수 있습니다.



제목 : classic asp 에서 업로드컴포넌트 dll 파일 생성법 및 사용법




소스 :


Option Explicit
Const ERR_INVALID_FILENAME = vbObjectError + 1000
Const ERR_INVALID_TARGET = vbObjectError + 1001
Const ERR_FILE_EXISTS = vbObjectError + 1002
Const ERR_UPLOAD_CALLED = vbObjectError + 1003
Const VB_ERR_PATH_NOT_FOUND = 76

Private m_objContext As ObjectContext
Private m_objRequest As ASPTypeLibrary.Request

Private m_strTarget As String
Private m_strFileName As String
Private m_blnOverWrite As Boolean
Private m_blnUploaded As Boolean
Private m_lngTotalBytes As Long

'All other form elements go here.
Private m_formCol As Scripting.Dictionary

Implements ObjectControl

Private Function ObjectControl_CanBePooled() As Boolean
ObjectControl_CanBePooled = False
End Function

Private Sub ObjectControl_Activate()
Set m_objContext = GetObjectContext()
Set m_objRequest = m_objContext("Request")
Set m_formCol = New Scripting.Dictionary
End Sub

Private Sub ObjectControl_Deactivate()
Set m_objContext = Nothing
Set m_objRequest = Nothing
Set m_formCol = Nothing
End Sub

Public Sub Upload()

Const DEFAULT_CHUNK_SIZE = 262144 '256kb

Dim bytBeginOfChunk() As Byte
Dim bytEndOfChunk() As Byte
Dim bytBeginOfName() As Byte
Dim bytEndOfName() As Byte
Dim bytBeginOfFile() As Byte
Dim bytEndOfFile() As Byte
Dim bytBeginOfValue() As Byte
Dim bytEndOfValue() As Byte
Dim bytName() As Byte
Dim bytValue() As Byte
Dim bytThisChunk() As Byte
Dim bytFileName() As Byte
Dim lngBeginOfChunk As Long
Dim lngEndOfChunk As Long

Dim lngBeginOfAttribute As Long
Dim lngEndOfAttribute As Long
Dim lngBeginOfValue As Long
Dim lngEndOfValue As Long
Dim blnEndOfData As Boolean
Dim lngChunkSize As Long
Dim lngBytesLeft As Long
Dim lngFileNum As Long
Dim strFileName As String

On Error GoTo UploadErr

If Uploaded Then
Err.Raise ERR_UPLOAD_CALLED, App.Title, "The Upload method has already been called."
End If

bytBeginOfChunk = StrConv("-----------------------------", vbFromUnicode)
bytEndOfChunk = StrConv("-----------------------------", vbFromUnicode)

bytBeginOfName = StrConv("name=", vbFromUnicode) & ChrB(34)
bytEndOfName = ChrB(34)

bytBeginOfFile = StrConv("filename=", vbFromUnicode) & ChrB(34)
bytEndOfFile = ChrB(34)

bytBeginOfValue = ChrB(13) & ChrB(10) & ChrB(13) & ChrB(10)
bytEndOfValue = ChrB(13) & ChrB(10) & StrConv("-----------------------------", vbFromUnicode)

'Initialize the chunk size.
If m_objRequest.TotalBytes <= DEFAULT_CHUNK_SIZE Then
lngChunkSize = m_objRequest.TotalBytes
Else
lngChunkSize = DEFAULT_CHUNK_SIZE
End If

'Get the chunk from the request object.
bytThisChunk = m_objRequest.BinaryRead(CVar(lngChunkSize))

'Initialize the value.
lngBeginOfChunk = 1

'Repeat until the end of the data.
Do While Not blnEndOfData
'Begin the chunk.
lngBeginOfChunk = InStrB(lngBeginOfChunk, bytThisChunk, bytBeginOfChunk) + UBound(bytBeginOfChunk)

'Get name of the item.
lngBeginOfAttribute = InStrB(lngBeginOfChunk, bytThisChunk, bytBeginOfName) + UBound(bytBeginOfName) + 1
lngEndOfAttribute = InStrB(lngBeginOfAttribute, bytThisChunk, bytEndOfName)
bytName = MidB(bytThisChunk, lngBeginOfAttribute, lngEndOfAttribute - lngBeginOfAttribute)

'Get the value of the item.
lngBeginOfValue = InStrB(lngEndOfAttribute, bytThisChunk, bytBeginOfValue, vbBinaryCompare) + UBound(bytBeginOfValue) + 1
lngEndOfValue = InStrB(lngBeginOfValue, bytThisChunk, bytEndOfValue, vbBinaryCompare)

If lngEndOfValue = 0 Then
'The item extends the past current chunk.
bytValue = MidB(bytThisChunk, lngBeginOfValue, lngChunkSize)
Else
'The item value exists in the current chunk.
bytValue = MidB(bytThisChunk, lngBeginOfValue, lngEndOfValue - lngBeginOfValue)
End If

If UCase(StrConv(bytName, vbUnicode)) = "FILE" Then
lngBeginOfAttribute = InStrB(lngBeginOfChunk, bytThisChunk, bytBeginOfFile, vbBinaryCompare) + UBound(bytBeginOfFile) + 1
lngEndOfAttribute = InStrB(lngBeginOfAttribute, bytThisChunk, bytEndOfFile, vbBinaryCompare)

bytFileName = MidB(bytThisChunk, lngBeginOfAttribute, lngEndOfAttribute - lngBeginOfAttribute)

If UBound(bytFileName) < 0 Or UBound(bytValue) < 0 Then
Err.Raise ERR_INVALID_FILENAME, App.Title, "Invalid File Name."
End If

If Me.Target = "" Then
Err.Raise ERR_INVALID_TARGET, App.Title, "Invalid Target."
End If

'Use the original file name.
If Me.FileName = "" Then

'Trim the path from the file name.
While InStrB(1, bytFileName, StrConv("\", vbFromUnicode), vbBinaryCompare) > 0
bytFileName = MidB(bytFileName, InStrB(1, bytFileName, StrConv("\", vbFromUnicode)) + 1)
Wend

'Set the property.
Me.FileName = StrConv(bytFileName, vbUnicode)

'Convert the byte to Unicode.
strFileName = Me.Target & Me.FileName

Else
strFileName = Me.Target & Me.FileName
End If

'Check for overwrite.
If Me.OverWrite Then
'This is the hack check. Make sure that wildcard characters cannot be used.
If Not InStr(1, strFileName, "*") Then
If FileExists(strFileName) Then
Kill strFileName
End If
Else
Err.Raise ERR_INVALID_FILENAME, App.Title, "The specified file name appears to be invalid."
End If
Else
If FileExists(strFileName) Then
Err.Raise ERR_FILE_EXISTS, App.Title, "The file already exists."
End If
End If

lngFileNum = FreeFile

Open strFileName For Binary Access Write As #lngFileNum

'Write the file to the destination directory.
Put #lngFileNum, , bytValue

'This chunk is empty. Therefore, get a new chunk.
lngBytesLeft = m_objRequest.TotalBytes - lngChunkSize

'Start the chunking machine.
Do While lngBytesLeft > 0

'Get a new chunk.
bytThisChunk = m_objRequest.BinaryRead(CVar(lngChunkSize))

lngEndOfValue = InStrB(1, bytThisChunk, bytEndOfValue, vbBinaryCompare)

If lngEndOfValue > 0 Then
'The item value exists in the current chunk.
bytThisChunk = MidB(bytThisChunk, 1, lngEndOfValue - 1)
End If

'Append the chunk to the file.
Put #lngFileNum, , bytThisChunk

lngBytesLeft = lngBytesLeft - lngChunkSize

If lngBytesLeft < lngChunkSize Then
lngChunkSize = lngBytesLeft
End If
Loop

Close #lngFileNum

TotalBytes = FileLen(strFileName)

' Exit Do
Else
If UCase(StrConv(bytName, vbUnicode)) = "SAVEAS" Then
Me.FileName = StrConv(bytValue, vbUnicode)
Else
'form field other than file, such as textboxes
If UBound(bytValue) > 0 And UBound(bytName) > 0 Then
m_formCol.Add StrConv(bytName, vbUnicode), StrConv(bytValue, vbUnicode)
Else
m_formCol.Add StrConv(bytName, vbUnicode), ""
End If
End If
End If

'Get the next chunk.
lngBeginOfChunk = lngEndOfValue

If InStrB(lngBeginOfChunk, bytThisChunk, bytBeginOfName, vbBinaryCompare) = 0 Then
blnEndOfData = True
End If
Loop

Uploaded = True

Exit Sub

UploadErr:

If Err.Number = VB_ERR_PATH_NOT_FOUND Then
Err.Raise ERR_INVALID_TARGET, App.Title, "The Target specified does not exist."
Else
Err.Raise Err.Number, Err.Source, Err.Description
End If
End Sub

Public Property Get Form() As Collection
Set Form = m_formCol
End Property
Public Property Get FileName() As String
FileName = m_strFileName
End Property

Public Property Let FileName(ByVal strNewValue As String)
If Uploaded Then
Err.Raise ERR_UPLOAD_CALLED, App.Title, "The Upload method has already been called."
Else
m_strFileName = strNewValue
End If
End Property

Public Property Get OverWrite() As Boolean
OverWrite = m_blnOverWrite
End Property

Public Property Let OverWrite(ByVal blnNewValue As Boolean)
If Uploaded Then
Err.Raise ERR_UPLOAD_CALLED, App.Title, "The Upload method has already been called."
Else
m_blnOverWrite = blnNewValue
End If
End Property

Private Property Get Uploaded() As Boolean
Uploaded = m_blnUploaded
End Property

Private Property Let Uploaded(ByVal blnNewValue As Boolean)
m_blnUploaded = blnNewValue
End Property

Public Property Get Target() As String
Target = m_strTarget
End Property

Public Property Let Target(ByVal NewValue As String)
If Uploaded Then
Err.Raise ERR_UPLOAD_CALLED, App.Title, "The Upload method has already been called."
Else
m_strTarget = NewValue
End If
End Property

Private Function FileExists(ByVal FileName As String) As Boolean
On Error GoTo FileExistsErr

FileLen FileName
FileExists = True
Exit Function

FileExistsErr:
If Err.Number = VB_ERR_PATH_NOT_FOUND Then
FileExists = False
End If
End Function

Public Property Get TotalBytes() As Long
TotalBytes = m_lngTotalBytes
End Property

Private Property Let TotalBytes(ByVal NewValue As Long)
m_lngTotalBytes = NewValue
End Property





내용 :


Microsoft Visual Basic 6 프로젝트 설정 및 코드

  1. Visual Basic 시작 하 고 새 ActiveX DLL 프로젝트를 시작 합니다.
  2. 프로젝트 이름
    ASPFileUpload.
  3. Class1 에 이름 변경
    파일입니다.
  4. 프로젝트 메뉴에서 참조를 클릭 합니다.
  5. 참조 대화 상자에서 다음 옵션을 선택한 다음 확인을 누릅니다.
    • Visual Basic 응용 프로그램에 대 한
    • Visual Basic 런타임 개체 및 프로시저
    • Visual Basic 개체 및 프로시저
    • OLE 자동화
    • COM + 서비스 형식 라이브러리
    • Microsoft Active Server Pages 개체 라이브러리
    • Microsoft 스크립팅 런타임
  6. 프로젝트 메뉴에서 ASPFileUpload 속성을 클릭 합니다.
  7. 프로젝트 속성 대화 상자에서 무인 실행 및 메모리에 보유선택 하 고 확인을 클릭 합니다.
  8. File.cls에 다음 코드를 붙여 넣습니다.







예제 소스 파일 :



<%@ Language=VBScript %><html>
<head>
</head>
<body>
<form enctype="multipart/form-data" action="uploadfile.asp" method="post" name="main1">
<input name="file" type="file" size="50">
<INPUT type="text" id=text1 name=text1><INPUT type="text" id=text2 name=text2>
<input name="submit" type="submit" value="Upload">
</form>
</body>
</html>
<%
'//////////////////////////////////////////////////////////////////////////////////
'// ASPFileUpload.File API
'//
'// Properties
'// FileName
'// - Read/Write
'// - The file will be saved with this file name.
'// - This property can only be set before calling Upload.
'// - If no value is specified, the original file name
'// - in the HTTP post will be used.
'//
'// OverWrite
'// - Read/Write
'// - This property can only be set before calling Upload.
'// - If set to false and if the destination file exists, an error
'// - is raised. The default value is False.
'//
'// Target
'// - Read/Write
'// - The file will be written to this folder.
'// - This property can only be set before calling Upload.
'// - There is no default value for this property and it is required.
'//
'// Form
'// - ReadOnly
'// - Scripting.Dictionary object
'// - Can access a specific item by using aspfileupload.Form("item").
'// - Acts like the asp form collection.
'// - Can enumerate all values in a collection with for each.
'// - Only filled after the Upload method is called.
'//
'// Methods
'// Upload
'// - This method parses the HTTP Post and writes the file.
'//
'// Other
'// - ASPFileUpload requires COM+
'// - Any call to the Request.Form() collection will cause the Upload
'// method to fail as the method references the Binary contents of the
'// Request object through the Request.BinaryRead method.
'// - Also, if you access a variable in the Request collection without
'// specifying the subcollection that it belongs to, the Request.Form collection
'// may be searched. This causes an error in the Upload method.
'//
'//////////////////////////////////////////////////////////////////////////////////

Dim strMsg 'As String

' On Error Resume Next
dim fuFile
set fuFile = server.CreateObject("aspFileupload.file")
'Set the destination folder.
fuFile.Target = "C:\TEMP\AspFileUpload\"
fuFile.Upload

If Err.number = 0 Then
strMsg = fuFile.FileName & " was uploaded successfully."
Else
strMsg = "An error occurred when uploading your file: " & Err.Description
End If
for each o in fuFile.Form
Response.Write o & "<BR>"

next

Response.Write fuFile.Form.item("text1") & " : " & fuFile.Form.item("text2")
' Response.Write Request.Form("test")
set fufile = nothing
%>
<html>
<head></head>
<body>
<%=strMsg%>
</body>
</html>
  1. 와 같이 업로드 된 파일을 수신 하는 웹 서버에 폴더를 만듭니다.
    C:\TEMP\AspFileUpload.
  2. ASPFileUpload.dll 파일을 웹 서버에 복사한 다음 명령 프롬프트에서 다음 명령을 사용 하 여 등록:
    regsvr32 PathToDLL\ASPFileUpload.dll
  3. 사용자에 게 파일을 업로드 하려면 파일 권한 (쓰기 액세스)을 적용 합니다.
  4. 시작을 누르고,
    설정을 누른 다음 제어판을 클릭 합니다.
  5. 제어판에서 관리 도구클릭 한 다음 구성 요소 서비스 Microsoft 관리 콘솔 (MMC)에서 구성 요소 서비스를 열려면 클릭 합니다.
  6. 구성 요소 서비스 노드를 확장 하면
    내 컴퓨터 노드를, 컴퓨터 노드 및 해당
    COM + 응용 프로그램 노드입니다.
  7. 노드를 마우스 오른쪽 단추로 클릭 하 고 새로 만들기를 가리킨 다음 클릭
    응용 프로그램.
  8. 설치 하거나 새 응용 프로그램 만들기 대화 상자에서 빈 응용 프로그램 만들기, 응용 프로그램 이름을 클릭 합니다. 서버 응용 프로그램을 선택 하 고 을 클릭 해야 합니다.
  9. 응용 프로그램 Id 설정 대화 상자에서 다음 사용자를 클릭 하 고 적절 한 사용자 계정에 대 한 자격 증명을 입력 합니다. 사용자 계정이 업로드 된 파일을 받을 폴더에 대 한 쓰기 권한이 있어야 합니다.
  10. 마침을 클릭합니다.
  11. 이 응용 프로그램에 대 한 방금 만든 노드를 확장 합니다.
  12. 해당 노드의 마우스 오른쪽 단추로 클릭 하 고 새로 만들기를 가리킨 다음 구성 요소를 클릭 합니다.
  13. 새 구성 요소 설치를 클릭 하 고 저장 하 고.dll 등록 되어 있는 폴더를 찾아 파일, 파일을 클릭 하 고 다음을 누르고
    마침.
  14. 웹 루트 폴더로 Postfile.asp 파일과 Uploadfile.asp 파일을 복사 합니다. 웹 루트 폴더는 기본적으로 C:\Inetpub\Wwwroot입니다.
  15. 1 단계에서 만든 폴더를 반영 하도록 Uploadfile.asp의 대상 폴더를 편집 합니다. 대상 폴더 할당은 코드의 다음 줄에 있습니다.
    fuFile.Target = "C:\TEMP\AspFileUpload\"


파일 업로드

  1. 웹 브라우저에서 다음 URL에서 Postfile.asp 페이지를 엽니다.
    http://YourWebServer/Postfile.asp
  2. 업로드를 누른 다음 원하는 파일을 선택 합니다.
    업로드합니다.
  3. 업로드 폴더를 확인 합니다. 업로드 한 파일이이 폴더에 나타납니다.

 출처 :  https://support.microsoft.com/ko-kr/help/299692/how-to-upload-files-to-a-web-server-by-using-asp




Comments