블로그 이미지
Magic_kit
study 관련자료를 한곳으로 자기 개발 목적으로 재태크 재무 관리 목적으로 일상생활의 팁을 공유 하기 위하여 블로그를 개설 하였습니다.

calendar

1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31

Category

Recent Post

Recent Comment

Archive

2009. 11. 2. 14:32 .Net Project/ASP.NET 3.5 Sp1
반응형

 Down.aspx.Cs


 
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.IO;

/// <summary>
/// BoardDown : 완성형(DotNetNote) 게시판의 강제다운로드 페이지
/// </summary>
public partial class DotNetNote_BoardDown : System.Web.UI.Page
{
  #region Private Member Variables
  private int intNum;
  private string strFileName = "";
  private string strBaseDir = "";
  #endregion

  #region Event Handlers
  protected void Page_Load(object sender, System.EventArgs e)
  {
    intNum = Convert.ToInt32(
      Request.QueryString["Num"].ToString());

    SqlConnection objCon = new SqlConnection(
        ConfigurationManager.ConnectionStrings[
          "ConnectionString"].ConnectionString);
    objCon.Open();
    SqlCommand objCmd = new SqlCommand(
        "Select FileName From DotNetNote Where Num = "
        + intNum.ToString(), objCon);
    strFileName = objCmd.ExecuteScalar().ToString();
    objCon.Close();

    // 다운로드 폴더 지정
    strBaseDir = Server.MapPath("./MyFiles/");
    if (strFileName == null)
    {
      Response.Redirect("./BoardList.aspx");
    }
    else
    {
      //강제 다운로드 창 띄우기 주요 로직
      Response.Clear();
      Response.ContentType = "application/octet-stream";
      Response.AddHeader("Content-Disposition", "attachment;filename="
        + Server.UrlPathEncode(strFileName));
      Response.WriteFile(strBaseDir + strFileName);
      UpdateDownCount();//
      Response.End();
    }
  }

  #endregion

  #region Private Methods
  // 다운수 증가 로직
  private void UpdateDownCount()
  {
    SqlConnection objCon = new SqlConnection();
    objCon.ConnectionString =
      ConfigurationManager.ConnectionStrings[
        "ConnectionString"].ConnectionString;
    objCon.Open();

    SqlCommand objCmd = new SqlCommand();
    objCmd.Connection = objCon;

    objCmd.CommandText =
      "Update DotNetNote Set DownCount = DownCount + 1 "
      + "Where FileName = '" + strFileName + "'";
    objCmd.CommandType = CommandType.Text;
    objCmd.ExecuteNonQuery();

    objCon.Close();
    objCon = null;
  }
  #endregion
}





반응형
posted by Magic_kit