ASP.NET

17.ASP.NET - 자료실 게시판(9) : Down.aspx

Godffs 2009. 10. 12. 12:17
반응형
Upload - Down.aspx

Down.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Down.aspx.cs" Inherits="Upload_Down" %>

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

 

<html xmlns="http://www.w3.org/1999/xhtml">

<head id="Head1" runat="server">

    <title></title>

</head>

<body>

    <form id="form1" runat="server">

    <div>

   

        <h3>Down.aspx UI 없는 페이지입니다.</h3>

        <div>

        Down.aspx?FileName=??? 넘겨오는 파일명이 files 폴더에 있으면

        강제 다운로드 시켜주는 페이지

        </div>

   

    </div>

    </form>

</body>

</html>


Down.aspx.cs

using System;

using System.IO;

 

public partial class Upload_Down : System.Web.UI.Page

{

    private string strFileName = String.Empty;//넘겨져온 파일명 저장

    private string strBaseDir = String.Empty;//어디에 저장할 폴더명

 

    protected void Page_Load(object sender, EventArgs e)

    {

        strFileName = Request.QueryString["FileName"].ToString();

        strBaseDir = Server.MapPath(".") + @"\files";

 

        if (strFileName == null)

        { //넘겨져온 파일명이 없다면...

            Response.End();//멈춤

        }

 

        else

        {         

            //강제 다운로드 : 아래 로직이 강제다운로드 로직입니다.  

            UpdateDownCount(); // 다운 카운트 증가

            Response.Clear();//버퍼 비우기

 

            Response.ContentType = "application/octet-stream";

 

            Response.AddHeader("Content-Disposition",

              "attachment;filename=" +

              Server.UrlPathEncode(strFileName));

 

            Response.WriteFile(

              Path.Combine(strBaseDir, strFileName));

 

            Response.End();//버퍼 비우고, 종료.

        }

    }

 

    private void UpdateDownCount()

    {

        UploadBiz ub = new UploadBiz();

 

        //넘겨진 파일명에 해당하는 DownCount 1 증가

        ub.UpdateDownCount(strFileName);

    }

}


결과화면

[그림17-1]



반응형