설명
DEXTUploadX5는 1.3.0.0 버전부터 압축 다운로드를 지원한다.
압축 다운로드는 여러 파일을 동시에 받기 위해서 서버에 대상 파일을 하나의 압축 파일로 묶도록 서버에 요청하고, 그 압축 파일을 내려받는다. 다른 예제에서 보았던, 단일/다중 파일 다운로드 예제와 달리, 압축 다운로드는 압축 과정을 서버에 일임하기 때문에, 대상 파일에 대한 다운로드 경로를 지정하는 속성(downUrl)에 영향을 받지 않는다.
압축 다운로드를 하려면, 압축을 담당하며, 압축된 파일을 다운로드할 수 있도록 그 경로값을 반환하는 주소를 setCompressURL 메소드를 사용하여 지정해야 한다. 그리고 단일/다중 파일 다운로드와 달리 압축 다운로드를 시작할 때에는 downloadCompressed 메소드를 호출한다.
var dx = dx5.get("컴포넌트 아이디");
// downUrl 속성이 불필요하다.
// 다만 압축 작업을 해야 하는 서버에서 압축 대상을 구별하기 위한 vindex 속성값을 반드시 지정해 주어야 한다.
// vindex는 일종의 유니크한 문자열 값으로써, 가상 파일을 구별하기 위한 유일한 수단이다.
dx.addVirtualFile({ vindex: "IDX0003", name: "서강대교_509147.jpg", size: 509147 });
dx.addVirtualFile({ vindex: "IDX0004", name: "우도해변_239826.jpg", size: 239826 });
dx.addVirtualFile({ vindex: "IDX0005", name: "코스모스 (빈공간) 195779.jpg", size: 195779 });
// 압축을 처리하고 압축 파일 다운로드 경로를 반환하는 주소를 설정한다.
dx.setCompressURL("http://도메인/경로../service/compress.do");
// 플래그 값에 따라 다운로드를 수행한다.
// AUTO: 가상 파일을 다운로드한다.
// SELECTED: 선택된 가상 파일을 다운로드한다.
// CHECKED: 체크된 가상 파일을 다운로드한다.
dx.downloadCompressed("SELECTED");
파일 압축과 다운로드는 compress.asp, zip-download.asp에서 처리된다.
DEXTUploadX5는 downloadCompressed 메소드를 호출하면, 압축 다운로드할 가상 파일의 vindex 속성값을 콤마(,)를 구분자로 하는 하나의 목록으로 하는 문자열 정보를 'DEXTUploadX5_VIndexes' 이름의 폼 데이터를 서버에 POST 형식으로 전송한다.
서버에서는 'DEXTUploadX5_VIndexes' 값을 얻어 콤마(,) 문자로 분리를 하여, 대상 정보를 얻게 된다.
압축이 되었다면, 반드시 대상 압축 파일을 다운로드할 수 있는 경로를 응답에 실어 클라이언트로 보내주어야 한다.
# compress.asp
dim vindexes, tempFolder, defaultFolder, fs, compressor, i, file, compressedPath
vindexes = Split(GetFormString("DEXTUploadX5_VIndexes", "", true), ",")
tempFolder = Request.ServerVariables("APPL_PHYSICAL_PATH") & "files\temp\"
defaultFolder = Request.ServerVariables("APPL_PHYSICAL_PATH") & "files\attach\"
set fs = Server.CreateObject("Scripting.FileSystemObject")
set compressor = Server.CreateObject("DEXT.DEXTProCompressor")
compressor.CompLevel = -1
for i = 0 to ubound(vindexes)
if vindexes(i) = "IDX0003" then
set file = fs.GetFile(defaultFolder & "서강대교_509147.jpg")
elseif vindexes(i) = "IDX0004" then
set file = fs.GetFile(defaultFolder & "우도해변_239826.jpg")
elseif vindexes(i) = "IDX0005" then
set file = fs.GetFile(defaultFolder & "코스모스 (빈공간) 195779.jpg")
else
set file = nothing
end if
if not file is nothing then
compressor.AddFile(file.Path)
if compressor.Count = 1 then
compressor.CompFileSavePath = tempFolder & GetFileNameWithoutExtension(file.Name) & ".zip"
end if
end if
set file = nothing
next
if compressor.Count > 0 then
compressedPath = compressor.Compress(true)
set file = fs.GetFile(compressedPath)
Response.ContentType = CONTENT_TYPE_TEXTPLAIN
Response.CharSet = UTF8_CHARSET
Response.Clear()
Response.Write(GetProtocolFromURL() & "://" & GetDomainFromURL() & ":" & GetPortFromURL() & _
"/service/zip-download.asp?name=" & Replace(Server.UrlEncode(file.Name), "+", "%20"))
set file = nothing
else
Response.AppendToLog("Targets for zip not found")
Response.Status = HTTP_FILE_NOT_FOUND_ERR
end if
set compressor = nothing
set fs = nothing
# zip-download.asp
dim fileName, tempFolder, fs, file, oDextpro
fileName = GetQueryString("name", "", true)
tempFolder = Request.ServerVariables("APPL_PHYSICAL_PATH") & "files\temp\"
set fs = Server.CreateObject("Scripting.FileSystemObject")
set file = fs.GetFile(tempFolder & fileName)
if file is nothing then
Response.AppendToLog("Zip file not found")
Response.Status = HTTP_FILE_NOT_FOUND_ERR
else
set oDextpro = Server.CreateObject("DEXT.FileDownload")
oDextpro.Download file.Path, file.Name, true, false
fs.DeleteFile(file.Path)
set oDextpro = nothing
end if
set file = nothing
set fs = nothing
파일을 압축하는 작업은 서버의 리소스(CPU, I/O)를 상당히 소모하는 작업이기 때문에, 크기가 큰 파일 또는 압축 파일의 대상이 되는 파일의 개수가 많은 경우, 서버에 상당한 오버헤드가 발생하므로, 상황에 따라 파일 압축 과정을 기다리는 시간이 길어질 수 있으며, 이는 세션이 끊기는 문제가 초래하거나, 서비스 자체가 중지되는 중대한 문제로 이어질 수 있다. 따라서 압축 다운로드을 하기 위한 정책적 제한을 수립 후, 기능을 사용하는 것을 권장한다.
예제
압축은 서버에서 진행된다. 압축이 진행되는 동안은 진행창이 표시되지만, 압축된 파일을 다운로드할 때는 단일 파일 다운로드로 전환되기 때문에 다운로드 과정에 대한 자체 진행창은 제공되지 않는다.