웹마스터 팁

예제 링크 : http://mygony.com/tt/index.php?pl=85&nc=1

다중 파일 다운로드를 스크립트 꼼수로 예전에 구현했었는데, XP SP2 버전과 IE 5.0 이하에서 문제가 있다고 하네요.
( 참고 : http://mygony.com/tt/index.php?pl=76&nc=1 )

이번 소스 역시 대단하다고 할만한 것은 못되지만, 조금 더 미려한(?) 화면을 보여줄 수 있다는 장점이 있고, (아마도) IE 5.0도 지원될 겁니다. -_-a;;

이 부분이 소스입니다.
-------------------------------------------------------------------
/**
* by 행복한고니 (20041014)
*/
function mdown(N, W, H, T, L)
{
    var Title = "Downloading..."; // 창 제목
    var Loading = "loading.html"; // 로딩 페이지 경로

    //-------------------------------------------------------
    // 이 이하로는 소스를 수정하지 마세요.
    //-------------------------------------------------------

    var objs = document.getElementsByName(N);
    var win, rowStr = "", S = [];
    
    if (objs == null || objs.length == undefined) return;
    if (W == undefined || isNaN(parseInt(W))) W = 300;
    if (H == undefined || isNaN(parseInt(H))) H = 300;
    if (T == undefined || isNaN(parseInt(T))) T = parseInt((screen.height-H-30)/2);
    if (L == undefined || isNaN(parseInt(L))) L = parseInt((screen.width-W)/2);

    for (var i=0; i<objs.length; i++) {
        if (objs[i].checked == undefined || !objs[i].checked) continue;
        rowStr += ",0";
        S[S.length] = mdown.GetURL(objs[i].value);
    }

    win = window.open("", "MDownWindow", "width="+W+",height="+H+",top="+T+",left="+L+",location=no,menubar=no,resizable=no,scrollbars=no,status=no");

    win.document.open();
    win.document.write(
        "<html><head><title>"+Title+"</title></head>" +
        "<frameset rows="100%"+rowStr+"" border="0" frameborder="0" noresize="">" +
        "<frame src=""+Loading+""></frame>"
    );

    for (var i=0; i < S.length; i++) {
        win.document.write("<frame src=""+S[i]+""></frame>");
    }
    win.document.write("</frameset>");
    win.document.close();
}

//----------------------------------------------------------------
// 사용자 정의 함수 구현부
//----------------------------------------------------------------
mdown.GetURL = function(val) {
    // 이 함수를 각자의 프로그램 사양에 따라 적당히 만들어주세요.
    // 체크박스에 있던 value 값이 val로 전달됩니다.

    return "http://mysite.com/download.php?fileid="+val+");";
}
-------------------------------------------------------------------

우선 파일 다운로드를 하는 프로그램을 download.php 라고 가정합니다.

이 때, download.php 에서 파일을 다운로드 할 때 download.php?fileid=아이디번호 와 같은 식으로 쓴다고 가정합니다. 이 규칙에 의해서 mdown.GetURL 함수를 사용자가 작성합니다.

저번의 iframe 버전에 비해 달라진 것이 하나 있는데 아무래도 새 창이다보니 새창의 위치를 지정해주는 전달자가 늘었습니다. 순서대로 W(idth : 너비), H(height : 높이), T(op : 위), L(eft : 왼쪽) 의 정수형 값을 지정해주시면 되고, 뒤쪽부터 순서대로 생략하거나 모두 생략할 수 있습니다. 생략하면 기본값이 지정됩니다.

스크립트를 포함한 후, 예제 HTML은 다음과 같이 쓸 수 있습니다.
<input type="checkbox" name="downfile" value="1"> 1번 파일
<input type="checkbox" name="downfile" value="2"> 2번 파일
<input type="checkbox" name="downfile" value="3"> 3번 파일
<input type="checkbox" name="downfile" value="4"> 4번 파일
<input type="button" value="다운로드" onclick="mdown('downfile')">

( IE 6.0 SP1, Firefox 1.0PR, Opera 7.54 ) - Windows2000