[Ajax] 파일 다운로드

.Net 2009. 1. 9. 14:26 posted by 무명시인

//파일다운로드
function fileUrl(fileUrl) {

    //요청 URL + 입력텍스트 값
    var Url = "BBC001D.aspx?Checkform=FileOpen&param=" + fileUrl;
   
    //XML Request 객체 선언및 Open
    var xmlRequest = new ActiveXObject("Microsoft.XMLHTTP");
    xmlRequest.open("POST", Url, false);

    //헤더값 설정
    xmlRequest.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");

    //요청
    xmlRequest.send(null);

    if (xmlRequest.ResponseText == "false") {
        alert("파일이 존재하지 않습니다.");
        return false;
    }
    __doPostBack('lnkBtnShow', '');
    location.href = xmlRequest.ResponseText;
   
}

#region ## Page_Load ##

    protected void Page_Load(object sender, EventArgs e)
    {
        if (Request.QueryString["Checkform"] == null) return;

        string strCheckform = Request.QueryString["Checkform"].ToString();
        string strStrID = Request.QueryString["param"].ToString();

        if (strCheckform.ToString().Equals("FileOpen")) FileCheck(strStrID);
    }

    #endregion

    #region ## FileCheck ##

    /// <summary>
    /// 파일 다운로드
    /// - 작  성  자 : 최성춘 (주)정원엔시스템
    /// - 최초작성일 : 2008.10.30
    /// - 최초수정자 :
    /// - 최초수정일 :
    /// - 주변경로그 :
    /// - 작업  내용 :
    /// </summary>
    private void FileCheck(string strStrID)
    {
        string[] strBoard = strStrID.ToString().Split('|');

        BBC001S obj = new BBC001S();

        DataSet ds = obj.GetBoardFileSelect(strBoard);

        bool blnFileExists = File.Exists(Server.MapPath(ds.Tables[0].Rows[0]["FilePath"].ToString()));

        if (blnFileExists)
        {           
            string[] arrExt = ds.Tables[0].Rows[0]["FilePath"].ToString().Split('.');

            if (arrExt[1].ToString().ToLower().Equals("gif")
                || arrExt[1].ToString().ToLower().Equals("jpg"))
            {
                Response.Redirect(ds.Tables[0].Rows[0]["FilePath"].ToString());
            }
            else
            {
                Response.Write(ds.Tables[0].Rows[0]["FilePath"].ToString());
            }

            Response.Flush();
            Response.End();           
        }
        else
        {       
            Response.Write("false");                       
            //Response.Redirect("Default.aspx");
        }
    }

    #endregion


아작스 포스트로 화일정보 넘겨주고 뿌려주기