4. BoardDown.aspx 페이지는 기본으로 두고 BoardDown.aspx.cs 파일을 열고 다음과 같이 입력한다. 이 코드 블록 안에 있는 내용은 특정 경로에 있는 파일을 강제로 내려받게 해주는 공식 같은 코드다. BoardDown.aspx 페이지는 넘어온 번호에 해당하는 파일을 지정된 폴더로부터 강제 다운로드시키고 다운로드 카운트를 1 증가시켜 주는 페이지다.

    ▼  ~/DotNetNote/BoardDown.aspx.cs

    using DotNetNote.Models;
    using System;
    
    namespace MemoEngine.DotNetNote
    {
      public partial class BoardDown : System.Web.UI.Page
      {
          private string fileName = ””;
          private string dir = ””;
          
          private NoteRepository _repository;
          public BoardDown()
          {
              _repository = new NoteRepository();
          }
          
          protected void Page_Load(object sender, EventArgs e)
          {
              // 넘어온 번호에 해당하는 파일명 가져오기(보안 때문에… 파일명 숨김)
              fileName =
                  _repository.GetFileNameById(Convert.ToInt32(Request[“Id”]));
              
              // 다운로드 폴더 지정 : 실제 사용 시 반드시 변경
              dir = Server.MapPath(”./MyFiles/”);
              if (fileName == null) // 특정 번호에 해당하는 첨부파일이 없다면,
              {
                  Response.Clear();
                  Response.End();
              }
              else
              {
                  // 다운로드 카운트 증가 메서드 호출
                  _repository.UpdateDownCount(fileName);
                  
                  //[!] 강제 다운로드 창 띄우기 주요 로직
                  Response.Clear();
                  Response.ContentType = “application/octet-stream”;
                  Response.AddHeader(“Content-Disposition”, “attachment;filename=”
                      + Server.UrlPathEncode(
                          (fileName.Length > 50) ?
                              fileName.Substring(fileName.Length - 50, 50) :
                                  fileName));
                  Response.WriteFile(dir + fileName);
                  Response.End();
              }
          }
      }
    }
    

    신간 소식 구독하기
    뉴스레터에 가입하시고 이메일로 신간 소식을 받아 보세요.