본문 바로가기

   
Programming/ASP.NET(4.0)

ASP.NET 파일 업로드 다운로드 게시판

반응형

ASP.NET 파일 업로드 다운로드 게시판

web.config

<?xml version="1.0"?>

<!--

  ASP.NET 응용 프로그램을 구성하는 방법에 대한 자세한 내용을 보려면

  http://go.microsoft.com/fwlink/?LinkId=169433 을 방문하십시오.

  -->

<configuration>

  <connectionStrings>

    <add name="conStr" connectionString="server=localhost;database=ASPNET;uid=sa;pwd=zangna1" providerName="System.Data.SqlClient"/>

  </connectionStrings>

  <system.web>

    <compilation debug="true" targetFramework="4.0"/>

 

         <!--파일용량 제한 풀기 maxRequestLength="" 기본적으로 시간은 90초이다.-->

         <httpRuntime requestValidationMode="2.0" maxRequestLength="1048576" executionTimeout="3600" />

        

    <pages theme="basic"/>

  </system.web>

  <system.webServer>

    <defaultDocument>

      <files>

        <add value="Board.aspx"/>

      </files>

    </defaultDocument>

  </system.webServer>

</configuration>


Add.aspx

<%@ Page Title="" Language="C#" MasterPageFile="~/Board.master" AutoEventWireup="true" CodeFile="Add.aspx.cs" Inherits="Add" ValidateRequest="false" %>

 

<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">

       </asp:Content>

 

<asp:Content ID="Content2" runat="server"

       contentplaceholderid="ContentPlaceHolder1">

       <table class="table" style="width:600px;">

             <tr>

                    <td align="center" bgcolor="Silver" width="150">

                           이름</td>

                    <td width="450">

                           &nbsp;<asp:TextBox ID="TextBoxName" runat="server" CssClass="txt" Width="100px"></asp:TextBox>

                           &nbsp;</td>

             </tr>

             <tr>

                    <td align="center" bgcolor="Silver" width="150">

                           이메일</td>

                    <td width="450">

                           &nbsp;<asp:TextBox ID="TextBoxEmail" runat="server" CssClass="txt" Width="250px"></asp:TextBox>

                           &nbsp;</td>

             </tr>

             <tr>

                    <td align="center" bgcolor="Silver" width="150">

                           제목</td>

                    <td width="450">

                           &nbsp;<asp:TextBox ID="TextBoxSubject" runat="server" CssClass="txt" Width="95%"></asp:TextBox>

                           &nbsp;</td>

             </tr>

             <tr>

                    <td align="center" bgcolor="Silver" width="150">

                           내용</td>

                    <td width="450">

                           &nbsp;<asp:TextBox ID="TextBoxContent" runat="server" CssClass="txt" Height="150px"

                                 TextMode="MultiLine" Width="95%"></asp:TextBox>

                           &nbsp;</td>

             </tr>

             <tr>

                    <td align="center" bgcolor="Silver" width="150">

                           태그적용</td>

                    <td width="450">

                           &nbsp;<asp:CheckBox ID="CheckBoxTag" runat="server" Text="HTML 태그 적용함" />

                           &nbsp;</td>

             </tr>

 

             <tr>

                    <td align="center" bgcolor="Silver" width="150">

                           첨부파일</td>

                    <td width="450">

                           &nbsp;&nbsp;<asp:FileUpload ID="FileUpload1"

                                 runat="server" Width="95%" />

                    </td>

             </tr>

 

 

             <tr>

                    <td align="center" bgcolor="Silver" width="150">

                           암호</td>

                    <td width="450">

                           &nbsp;<asp:TextBox ID="TextBoxPwd" runat="server" CssClass="txt" TextMode="Password"

                                 Width="100px"></asp:TextBox>

                           &nbsp;</td>

             </tr>

       </table>

       <div style="text-align:center;margin:10px;">

             <asp:ImageButton ID="ImageButton1" runat="server" ImageUrl="~/images/list.gif"

                    onclick="ImageButton1_Click" ValidationGroup="list" />

             <asp:ImageButton ID="ImageButton2" runat="server" ImageUrl="~/images/write.gif"

                    onclick="ImageButton2_Click" />

             <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server"

                    ControlToValidate="TextBoxName" Display="None" ErrorMessage="이름을 입력하세요!"

                    SetFocusOnError="True"></asp:RequiredFieldValidator>

             <asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server"

                    ControlToValidate="TextBoxEmail" Display="None" ErrorMessage="제목을 입력하세요"

                    SetFocusOnError="True"></asp:RequiredFieldValidator>

             <asp:RequiredFieldValidator ID="RequiredFieldValidator3" runat="server"

                    ControlToValidate="TextBoxSubject" Display="None" ErrorMessage="내용을 입력하세요"

                    SetFocusOnError="True"></asp:RequiredFieldValidator>

             <asp:RequiredFieldValidator ID="RequiredFieldValidator4" runat="server"

                    ControlToValidate="TextBoxPwd" Display="None" ErrorMessage="암호를 입력하세요"

                    SetFocusOnError="True"></asp:RequiredFieldValidator>

             <asp:ValidationSummary ID="ValidationSummary1" runat="server"

                    ShowMessageBox="True" ShowSummary="False" />

       </div>

</asp:Content>

 

 

 

 

Add.cs

using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Data;

using System.Data.SqlClient;

using System.Configuration;

using System.IO;

 

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

{

    protected void Page_Load(object sender, EventArgs e)

    {

 

    }

       protected void ImageButton1_Click(object sender, ImageClickEventArgs e)

       {

             //List

             Response.Redirect("List.aspx");

       }

       protected void ImageButton2_Click(object sender, ImageClickEventArgs e)

       {

             //1. 파일업로드

             string filename = "";

 

             if (FileUpload1.HasFile)

             {

                    filename = Path.GetFileName(FileUpload1.FileName);

                    FileUpload1.PostedFile.SaveAs(Server.MapPath("files/") + filename);

             }

 

             //2. 글쓰기(insert)

 

             //Write

             SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["conStr"].ConnectionString);

 

             SqlCommand cmd = new SqlCommand();

             cmd.Connection = con;

             cmd.CommandText = "INSERT INTO Board (name, email, subject, content, tag, pwd, userIP, filename) VALUES (@name, @email, @subject, @content, @tag, @pwd, @userIP, @filename)";

 

             //매개변수

             cmd.Parameters.Add("@name", SqlDbType.NVarChar, 20);

             cmd.Parameters.Add("@email", SqlDbType.VarChar, 50);

             cmd.Parameters.Add("@subject", SqlDbType.NVarChar, 100);

             cmd.Parameters.Add("@content", SqlDbType.NVarChar, 3000);

             cmd.Parameters.Add("@tag", SqlDbType.Char, 1);

             cmd.Parameters.Add("@pwd", SqlDbType.VarChar, 20);

             cmd.Parameters.Add("@userIP", SqlDbType.VarChar, 15);

             cmd.Parameters.Add("@filename", SqlDbType.VarChar, 100);

 

             //

             cmd.Parameters["@name"].Value = TextBoxName.Text;

             cmd.Parameters["@email"].Value = TextBoxEmail.Text;

             cmd.Parameters["@subject"].Value = TextBoxSubject.Text;

             cmd.Parameters["@content"].Value = TextBoxContent.Text;

             cmd.Parameters["@tag"].Value = CheckBoxTag.Checked ? "y" : "n";

             cmd.Parameters["@pwd"].Value = TextBoxPwd.Text;

             cmd.Parameters["@userIP"].Value = Request.UserHostAddress;

             cmd.Parameters["@filename"].Value = filename;

 

             con.Open();

             cmd.ExecuteNonQuery();

             con.Close();

 

             string script = @"<script type='text/javascript'>

                                              alert('글쓰기 완료!!');

                                              location.href = 'List.aspx';

                                          </script>";

 

             this.ClientScript.RegisterClientScriptBlock(this.GetType(), "add", script);

       }

}

 



Del.aspx

<%@ Page Title="" Language="C#" MasterPageFile="~/Board.master" AutoEventWireup="true" CodeFile="Del.aspx.cs" Inherits="Del" %>

 

<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">

</asp:Content>

<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">

       <table class="table" style="width:600px;" align="center">

             <tr>

                    <td align="center" bgcolor="Silver" width="150" >

                           암호</td>

                    <td width="150">

                           &nbsp;<asp:TextBox ID="TextBoxPwd" runat="server" CssClass="txt" Width="100px"></asp:TextBox>

                           &nbsp;</td>

             </tr>

       </table>

 

       <div style="text-align:center;margin:10px;">

             <asp:ImageButton ID="ImageButton1" runat="server"

                    ImageUrl="~/images/delete.gif" onclick="ImageButton1_Click" />

             <asp:ImageButton ID="ImageButton2" runat="server"

                    ImageUrl="~/images/back.gif" onclick="ImageButton2_Click" />

       </div>

 

</asp:Content>

 

 

 

Del.cs

using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Data;

using System.Data.SqlClient;

using System.Configuration;

using System.IO;

 

 

 

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

{

    protected void Page_Load(object sender, EventArgs e)

    {

 

    }

       protected void ImageButton1_Click(object sender, ImageClickEventArgs e)

       {

             //일치한다면 수정 작업 진행

             SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["conStr"].ConnectionString);

 

             SqlCommand cmd = new SqlCommand();

             cmd.Connection = con;

 

             cmd.CommandText = "SELECT COUNT(*) FROM Board WHERE seq=@seq AND pwd=@pwd";

 

             //같은 번호이기 때문에 파라메터 하나만 던져도 가능하다.

             cmd.Parameters.Add("@seq", SqlDbType.Int);

             cmd.Parameters["@seq"].Value = Request.QueryString["seq"];

 

             cmd.Parameters.Add("@pwd", SqlDbType.VarChar, 20);

             cmd.Parameters["@pwd"].Value = TextBoxPwd.Text;

 

             con.Open();

 

             int result = (int)cmd.ExecuteScalar();

 

             string script = "";

 

             if (result ==1)

             {

                    //첨부파일이 존재하면 삭제!!

                    cmd.CommandText = "SELECT fileName FROM Board WHERE seq=@seq";

 

                    string fileName = cmd.ExecuteScalar().ToString();

 

                    if (fileName != "")

                    {

                           FileInfo file = new FileInfo(Server.MapPath("files/") + fileName);

                           file.Delete();//첨부파일 삭제

                    }

 

                    cmd.CommandText = @"DELETE FROM Board

                                                     WHERE seq = @seq";

                   

                    cmd.ExecuteNonQuery();

 

                    script = @"<script type='text/javascript'>

                                              alert('삭제 완료!!');

                                              location.href = 'List.aspx';

                                          </script>";

             }

 

             else

             {

                    //아니면 안내메시지 출력

                    script = @"<script type='text/javascript'>

                                        alert('암호가 일치하지 않습니다!!');

                                    </script>";

             }

 

             con.Close();

 

             this.ClientScript.RegisterClientScriptBlock(this.GetType(), "edit", script);

       }

       protected void ImageButton2_Click(object sender, ImageClickEventArgs e)

       {

             string script = @"<script type='text/javascript'>

                                              location.href = 'List.aspx';

                                    </script>";

 

             this.ClientScript.RegisterClientScriptBlock(this.GetType(), "edit", script);

       }

}

 다운로드 횟수에 정확한 판별을 위하여 뷰페이지에서 <a href 태그로 다운로드.aspx를 들러서 처리토록 함.


View.aspx

<%@ Page Title="" Language="C#" MasterPageFile="~/Board.master" AutoEventWireup="true" CodeFile="View.aspx.cs" Inherits="View" %>

 

<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">

</asp:Content>

<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">

       <table class="table" style="width:600px;">

             <tr>

                    <td align="center" bgcolor="Silver" width="150">

                           이름</td>

                    <td width="450">

                           &nbsp;&nbsp;<asp:Label ID="LabelName" runat="server"></asp:Label>

                    </td>

             </tr>

             <tr>

                    <td align="center" bgcolor="Silver" width="150">

                           이메일</td>

                    <td width="450">

                           &nbsp;&nbsp;<asp:Label ID="LabelEmail" runat="server"></asp:Label>

                    </td>

             </tr>

             <tr>

                    <td align="center" bgcolor="Silver" width="150">

                           제목</td>

                    <td width="450">

                           &nbsp;&nbsp;<asp:Label ID="LabelTitle" runat="server"></asp:Label>

                    </td>

             </tr>

             <tr>

                    <td align="center" bgcolor="Silver" width="150" style="padding:10px;">

                           내용</td>

                    <td width="450">

                           <asp:Label ID="LabelContent" runat="server"></asp:Label>

                    </td>

             </tr>

             <tr>

                    <td align="center" bgcolor="Silver" width="150">

                           번호</td>

                    <td width="450">

                           &nbsp;&nbsp;<asp:Label ID="LabelSeq" runat="server"></asp:Label>

                    </td>

             </tr>

             <tr>

                    <td align="center" bgcolor="Silver" width="150">

                           읽음</td>

                    <td width="450">

                           &nbsp;&nbsp;<asp:Label ID="LabelReadCount" runat="server"></asp:Label>

                    </td>

             </tr>

             <tr>

                    <td align="center" bgcolor="Silver" width="150">

                           날짜</td>

                    <td width="450">

                           &nbsp;&nbsp;<asp:Label ID="LabelDate" runat="server"></asp:Label>

                    </td>

             </tr>

             <tr>

                    <td align="center" bgcolor="Silver" width="150">

                           첨부파일</td>

                    <td width="450">

                           &nbsp;&nbsp;<asp:Label ID="LabelFile" runat="server" Text="LabelFile"></asp:Label>

                    </td>

             </tr>

 

       </table>

       <div style="text-align:center;margin:10px;">

             <asp:ImageButton ID="ImageButton1" runat="server" ImageUrl="~/images/list.gif"

                    onclick="ImageButton1_Click" ValidationGroup="list" style="width: 39px" />

             <asp:ImageButton ID="ImageButton2" runat="server"

                    ImageUrl="~/images/modify.gif" onclick="ImageButton2_Click"

                    style="width: 39px" />

             <asp:ImageButton ID="ImageButton3" runat="server"

                    ImageUrl="~/images/delete.gif" onclick="ImageButton3_Click" />

       </div>

</asp:Content>

 

View.cs

using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Web.UI;

using System.Web.UI.WebControls;

 

using System.Data;

using System.Data.SqlClient;

using System.Configuration;

using System.IO;

 

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

{

    protected void Page_Load(object sender, EventArgs e)

    {

             if (!IsPostBack)

             {

                    //seq의 상세보기

                    SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["conStr"].ConnectionString);

 

                    SqlCommand cmd = new SqlCommand();

                    cmd.Connection = con;

                    cmd.CommandText = "UPDATE Board SET readCount = readCount + 1 WHERE seq=@seq;";

 

                    //같은 번호이기 때문에 파라메터 하나만 던져도 가능하다.

                    cmd.Parameters.Add("@seq", SqlDbType.Int);

                    cmd.Parameters["@seq"].Value = Request.QueryString["seq"];

 

                    con.Open();

 

                    if (Request.Cookies["read"] == null || Request.Cookies["read"].Value == "n")

                    {

                           //조회수 증가

                           cmd.ExecuteNonQuery();

                           Response.Cookies["read"].Value = "y";

                    }

                   

 

                    cmd.CommandText = "select * from Board where seq=@seq";

 

 

                    //결과값으로 처리용도지만 ExecuteNonQuery 용도로도 사용할수는 있다.

                    SqlDataReader reader = cmd.ExecuteReader();

                    if (reader.Read())

                    {

                           //출력

                           LabelName.Text = string.Format("{0} [{1}]", reader["name"].ToString(), reader["userIP"].ToString());

                           LabelEmail.Text = reader["email"].ToString();

                           LabelTitle.Text = reader["subject"].ToString();

 

                           //글 내용

                           string content = reader["content"].ToString();

                          

                           //\r\n = 엔터값 출력(웹페이지) \r\n 출력 방식이 틀려서 엔터값이 안먹는다.<br />

                           //DB에는 \r\n으로 들어가 있다. 원본은 회손하지 않고 처리해야 한다.

 

                           //엔터값처리 -> <br />

                           content = content.Replace("\r\n", "<br />");

 

                           //첨부된 파일이 이미지라면.. 글내용에 첨부파일을 자동으로 게시

                           //첨부된 파일이 이미지라면.. 글내용에 첨부파일을 자동으로 게시

                           if (reader["fileName"].ToString() != "")

                           {

                                 if ((Path.GetExtension(reader["fileName"].ToString()).ToLower() == ".gif") || (Path.GetExtension(reader["fileName"].ToString()).ToLower() == ".jpg") || (Path.GetExtension(reader["fileName"].ToString()).ToLower() == ".jpeg") || (Path.GetExtension(reader["fileName"].ToString()).ToLower() == ".png"))

                                 {

                                        string img = string.Format("<div style='margin:20px;text-align:center'><img src='files/{0}' /></div>", reader["fileName"].ToString());

                                        content = img + content;

                                 }

                           }

 

                           //태그 적용 안함 처리 ( < - > &lt;)

                           if (reader["tag"].ToString() == "n")

                           {

                                 Server.HtmlEncode(content);

                           }

 

                           //<script> 무조건 적용안함

                           content = content.Replace("<script>", "&ltscript&gt").Replace("</script>", "&lt/script>");

                           //Response.Write(content);

 

                           LabelContent.Text = content;

                           LabelSeq.Text = reader["seq"].ToString();

                           LabelReadCount.Text = reader["readCount"].ToString();

                           LabelDate.Text = reader["regDate"].ToString();

                    }

 

                    //첨부파일 처리

                    if (reader["fileName"].ToString() != "")

                    {

                           LabelFile.Text = string.Format("<a href='Download.aspx?seq={2}&file={0}'>{0}</a> [다운로드횟수:{1}]", reader["fileName"].ToString(), reader["downloadCount"].ToString(), Request.QueryString["seq"]);

                    }

 

                    else

                    {

                           LabelFile.Text = "첨부파일 없음";

                    }

 

                    reader.Close();

                    con.Close();

             }

    }

       protected void ImageButton1_Click(object sender, ImageClickEventArgs e)

       {

             //List

             Response.Redirect("List.aspx");

       }

      

       protected void ImageButton3_Click(object sender, ImageClickEventArgs e)

       {

             //삭제 Del.aspx?seq=5

             Response.Redirect("Del.aspx?seq=" + Request.QueryString["seq"]);

       }

 

       protected void ImageButton2_Click(object sender, ImageClickEventArgs e)

       {

             //수정

             Response.Redirect("Edit.aspx?seq=" + Request.QueryString["seq"]);

       }

}

 


Download.aspx

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

 

<!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 runat="server">

    <title></title>

</head>

<body>

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

    <div>

   

    </div>

    </form>

</body>

</html>

 

 

Download.cs

using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Text;

using System.Data;

using System.Data.SqlClient;

using System.Configuration;

 

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

{

    protected void Page_Load(object sender, EventArgs e)

    {

             SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["conStr"].ConnectionString);

 

             SqlCommand cmd = new SqlCommand();

             cmd.Connection = con;

             cmd.CommandText = "update Board set downloadCount = downloadCount + 1 where seq=@seq;";

            

             cmd.Parameters.Add("@seq", SqlDbType.Int);

             cmd.Parameters["@seq"].Value = Request.QueryString["seq"];

 

             con.Open();

             cmd.ExecuteNonQuery();

             con.Close();

 

             Response.Clear();//임시 페이지의 내용을 모두 지워라

 

             //Download.aspx?file=help.gif

             //Download.aspx?file=board.rar

             string file = Request.QueryString["file"];

 

             //Application/UnKnown : MIME(알수없는 형식)

             Response.ContentType = "Application/UnKnown";

 

             //다운로드 안치고 인식됨

             //Response.ContentType = "Image/GIF";

             Response.AddHeader("Content-Disposition", "Attachment;filename=" + HttpUtility.UrlEncode(file, Encoding.UTF8).Replace("+", "%20"));

 

             //해당 파일을 임시 페이지(출력 스트림)에 직접 쓴다.

             Response.WriteFile("files/" + file);

             Response.End();

 

    }

}

 



Edit.aspx

<%@ Page Title="" Language="C#" MasterPageFile="~/Board.master" AutoEventWireup="true" CodeFile="Edit.aspx.cs" Inherits="Edit" ValidateRequest="false" %>

 

<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">

</asp:Content>

<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">

       <table class="table" style="width:600px;">

       <tr>

             <td align="center" bgcolor="Silver" width="150">

                           이름</td>

             <td width="450">

                           &nbsp;<asp:TextBox ID="TextBoxName" runat="server" CssClass="txt" Width="100px"></asp:TextBox>

                           &nbsp;</td>

       </tr>

       <tr>

             <td align="center" bgcolor="Silver" width="150">

                           이메일</td>

             <td width="450">

                           &nbsp;<asp:TextBox ID="TextBoxEmail" runat="server" CssClass="txt" Width="250px"></asp:TextBox>

                           &nbsp;</td>

       </tr>

       <tr>

             <td align="center" bgcolor="Silver" width="150">

                           제목</td>

             <td width="450">

                           &nbsp;<asp:TextBox ID="TextBoxSubject" runat="server" CssClass="txt" Width="95%"></asp:TextBox>

                           &nbsp;</td>

       </tr>

       <tr>

             <td align="center" bgcolor="Silver" width="150">

                           내용</td>

             <td width="450">

                           &nbsp;<asp:TextBox ID="TextBoxContent" runat="server" CssClass="txt" Height="150px"

                                 TextMode="MultiLine" Width="95%"></asp:TextBox>

                           &nbsp;</td>

       </tr>

       <tr>

             <td align="center" bgcolor="Silver" width="150">

                           태그적용</td>

             <td width="450">

                           &nbsp;<asp:CheckBox ID="CheckBoxTag" runat="server" Text="HTML 태그 적용함" />

                           &nbsp;</td>

       </tr>

       <tr>

             <td align="center" bgcolor="Silver" width="150">

                           첨부파일</td>

             <td width="450">

                           &nbsp;&nbsp;<asp:FileUpload ID="FileUpload1"

                                 runat="server" />

                           <asp:Label ID="LabelFile" runat="server" Text="LabelFile"></asp:Label>

             </td>

       </tr>

       <tr>

             <td align="center" bgcolor="Silver" width="150">

                           암호</td>

             <td width="450">

                           &nbsp;<asp:TextBox ID="TextBoxPwd" runat="server" CssClass="txt" TextMode="Password"

                                 Width="100px"></asp:TextBox>

                           &nbsp;</td>

       </tr>

</table>

<div style="text-align:center;margin:10px;">

       <asp:ImageButton ID="ImageButton1" runat="server" ImageUrl="~/images/back.gif"

                    onclick="ImageButton1_Click" ValidationGroup="list" />

       <asp:ImageButton ID="ImageButton2" runat="server" ImageUrl="~/images/modify.gif"

                    onclick="ImageButton2_Click" />

       <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server"

                    ControlToValidate="TextBoxName" Display="None" ErrorMessage="이름을 입력하세요!"

                    SetFocusOnError="True"></asp:RequiredFieldValidator>

       <asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server"

                    ControlToValidate="TextBoxEmail" Display="None" ErrorMessage="제목을 입력하세요"

                    SetFocusOnError="True"></asp:RequiredFieldValidator>

       <asp:RequiredFieldValidator ID="RequiredFieldValidator3" runat="server"

                    ControlToValidate="TextBoxSubject" Display="None" ErrorMessage="내용을 입력하세요"

                    SetFocusOnError="True"></asp:RequiredFieldValidator>

       <asp:RequiredFieldValidator ID="RequiredFieldValidator4" runat="server"

                    ControlToValidate="TextBoxPwd" Display="None" ErrorMessage="암호를 입력하세요"

                    SetFocusOnError="True"></asp:RequiredFieldValidator>

       <asp:ValidationSummary ID="ValidationSummary1" runat="server"

                    ShowMessageBox="True" ShowSummary="False" />

</div>

</asp:Content>

 

 

 

Edit.cs

using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Data;

using System.Data.SqlClient;

using System.Configuration;

using System.IO;

 

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

{

    protected void Page_Load(object sender, EventArgs e)

    {

              if (!IsPostBack)

             {

                    //seq의 상세보기

                    SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["conStr"].ConnectionString);

 

                    SqlCommand cmd = new SqlCommand();

                    cmd.Connection = con;

                    cmd.CommandText = "select * from Board where seq=@seq";

 

                    //같은 번호이기 때문에 파라메터 하나만 던져도 가능하다.

                    cmd.Parameters.Add("@seq", SqlDbType.Int);

                    cmd.Parameters["@seq"].Value = Request.QueryString["seq"];

 

                    con.Open();

 

 

                    SqlDataReader reader = cmd.ExecuteReader();

                    if (reader.Read())

                    {

                           //출력

                           TextBoxName.Text = reader["name"].ToString();

                           TextBoxEmail.Text = reader["email"].ToString();

                           TextBoxSubject.Text = reader["subject"].ToString();

                           TextBoxContent.Text = reader["content"].ToString();

                           CheckBoxTag.Checked = reader["tag"].ToString() == "y" ? true : false;

 

                           //첨부파일 처리

                           if (reader["fileName"].ToString() != null)

                           {

                                 LabelFile.Text = string.Format("{0} 첨부됨.", reader["fileName"].ToString());

                           }

 

                           else

                           {

                                 LabelFile.Text = "첨부 파일 없음";

                           }

                    }

 

                    reader.Close();

                    con.Close();

             }

    }

       protected void ImageButton1_Click(object sender, ImageClickEventArgs e)

       {

 

       }

       protected void ImageButton2_Click(object sender, ImageClickEventArgs e)

       {

             //수정하기 암호가 일치하는지 확인

             //일치한다면 수정 작업 진행

             SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["conStr"].ConnectionString);

 

             SqlCommand cmd = new SqlCommand();

             cmd.Connection = con;

 

             cmd.CommandText = "SELECT COUNT(*) FROM Board WHERE seq=@seq AND pwd=@pwd";

 

             //같은 번호이기 때문에 파라메터 하나만 던져도 가능하다.

             cmd.Parameters.Add("@seq", SqlDbType.Int);

             cmd.Parameters["@seq"].Value = Request.QueryString["seq"];

 

             cmd.Parameters.Add("@pwd", SqlDbType.VarChar, 20);

             cmd.Parameters["@pwd"].Value = TextBoxPwd.Text;

 

             con.Open();

 

             int result = (int)cmd.ExecuteScalar();

 

             string script = "";

 

             if (result ==1)

             {

                    //첨부 파일을 수정?

                    string fileName = "";

 

                    if (FileUpload1.HasFile)

                    {

                           //기존 첨부 파일의 유무

                           if (LabelFile.Text != "없음")

                           {

                                 //있을 경우.. 기존 파일 삭제

                                 FileInfo file = new FileInfo(Server.MapPath("files/") + LabelFile.Text.Replace("첨부됨.", ""));

 

                                 //기존 첨부파일 삭제

                                 file.Delete();

                           }

 

                           //새로운 첨부파일을 추가

                           fileName = Path.GetFileName(FileUpload1.FileName);

                           FileUpload1.PostedFile.SaveAs(Server.MapPath("files/") + fileName);

                    }

 

                    else

                    {

                           if (LabelFile.Text != "없음")

                           {

                                 fileName = LabelFile.Text.Replace(" 첨부됨", "");

                           }

                    }

 

 

                    cmd.CommandText = @"UPDATE Board SET

                                                     name=@name,

                                                     email=@email,

                                                     subject=@subject,

                                                     content=@content,

                                                     tag=@tag,

                                                     userIP=@userIP,

                                                     regDate=getdate(),

                                                     fileName=@fileName

                                                     WHERE seq = @seq";

 

                    cmd.Parameters.Add("@name", SqlDbType.NVarChar, 20);

                    cmd.Parameters.Add("@email", SqlDbType.VarChar, 50);

                    cmd.Parameters.Add("@subject", SqlDbType.NVarChar, 100);

                    cmd.Parameters.Add("@content", SqlDbType.NVarChar, 3000);

                    cmd.Parameters.Add("@tag", SqlDbType.Char, 1);

                    cmd.Parameters.Add("@userIP", SqlDbType.VarChar, 15);

                    cmd.Parameters.Add("@fileName", SqlDbType.VarChar, 100);

 

                    //

                    cmd.Parameters["@name"].Value = TextBoxName.Text;

                    cmd.Parameters["@email"].Value = TextBoxEmail.Text;

                    cmd.Parameters["@subject"].Value = TextBoxSubject.Text;

                    cmd.Parameters["@content"].Value = TextBoxContent.Text;

                    cmd.Parameters["@tag"].Value = CheckBoxTag.Checked ? "y" : "n";

                    cmd.Parameters["@userIP"].Value = Request.UserHostAddress.ToString();

                    cmd.Parameters["@fileName"].Value = fileName;

 

                    cmd.ExecuteNonQuery();

 

                    script = @"<script type='text/javascript'>

                                        alert('수정 완료!!');

                                        location.href='List.aspx';

                                    </script>";

             }

 

             else

             {

                    //아니면 안내메시지 출력

                    script = @"<script type='text/javascript'>

                                              alert('암호가 일치하지 않습니다.');

                                          </script>";

             }

 

             con.Close();

 

             this.ClientScript.RegisterClientScriptBlock(this.GetType(), "edit", script);

 

 

 

            

       }

}

 


List.aspx

<%@ Page Title="" Language="C#" MasterPageFile="~/Board.master" AutoEventWireup="true" CodeFile="List.aspx.cs" Inherits="List" Trace="true" %>

 

<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">

</asp:Content>

<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">

<div style="text-align:center;margin:10px;">

       <asp:GridView ID="GridView1" runat="server" AllowPaging="True"

             AutoGenerateColumns="False" DataKeyNames="seq" DataSourceID="SqlDataSource1"

             onrowdatabound="GridView1_RowDataBound" Width="600px">

             <Columns>

                    <asp:BoundField DataField="seq" HeaderText="번호" InsertVisible="False"

                           ReadOnly="True" SortExpression="seq">

                    <ItemStyle HorizontalAlign="Center" Width="50px" />

                    </asp:BoundField>

                    <asp:HyperLinkField DataNavigateUrlFields="seq"

                           DataNavigateUrlFormatString="View.aspx?seq={0}" DataTextField="subject"

                           HeaderText="제목">

                    <ItemStyle Width="330px" />

                    </asp:HyperLinkField>

                    <asp:BoundField DataField="name" HeaderText="이름" SortExpression="name">

                    <ItemStyle HorizontalAlign="Center" Width="80px" />

                    </asp:BoundField>

                    <asp:BoundField DataField="regDate" DataFormatString="{0:yyyy-MM-dd}"

                           HeaderText="날짜" SortExpression="regDate">

                    <ItemStyle HorizontalAlign="Center" Width="80px" />

                    </asp:BoundField>

                    <asp:BoundField DataField="readCount" HeaderText="읽음"

                           SortExpression="readCount">

                    <ItemStyle HorizontalAlign="Center" Width="50px" />

                    </asp:BoundField>

             </Columns>

             <EmptyDataTemplate>

                    현재 등록된 게시물이 없습니다.

             </EmptyDataTemplate>

             <HeaderStyle BorderStyle="Solid" BorderWidth="1px" />

       </asp:GridView>

       <asp:SqlDataSource ID="SqlDataSource1" runat="server"

             ConnectionString="<%$ ConnectionStrings:conStr %>"

             SelectCommand="SELECT [seq], [name], [subject], [readCount], [regDate], [email], [fileName], [downloadCount] FROM [Board] ORDER BY [regDate] DESC">

       </asp:SqlDataSource>

       <asp:ImageButton ID="ImageButton1" runat="server" ImageUrl="~/images/write.gif"

             onclick="ImageButton1_Click" />

</div>

</asp:Content>

 

 

 

List.cs

using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Data;

 

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

{

    protected void Page_Load(object sender, EventArgs e)

    {

             if (!IsPostBack)

             {

                    //조회수 증가용 쿠키 할당 쿠키가 n일경우 무조건 카운트 증가 준비 완료~

                    Response.Cookies["read"].Value = "n";

             }                  

    }

       protected void ImageButton1_Click(object sender, ImageClickEventArgs e)

       {

             Response.Redirect("Add.aspx");

       }

       protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)

       {

             if (e.Row.RowType == DataControlRowType.DataRow)

             {

                    //헤더는 제외하고 현재 이벤트 발생이 내가 바인딩된 타입이냐?

                    //if(!ispostback) 과 비슷하게 무조건 사용하자.

                    //e.Row = 한행이라는 뜻이며 GrideViewRow x5 Trace키면 보이는 추적페이지에서 각셀들에 대한정보가 있으며 이러한 것들은 배열로 관리된다.

                    //1. 제목이 길면.. 자르기

                    HyperLink subject = e.Row.Cells[1].Controls[0] as HyperLink;

 

                    if (subject.Text.Length > 20)

                    {

                           subject.Text = subject.Text.Substring(0, 20) + "...";

                    }

 

                    //현재 바인딩되거나 바인딩된 데이터에 접근하는 방법

                    //A. 출력된 결과값에 접근하는 방법

                    //바운드된 필드(셀을 뒤지면 언제든지 출력된 결과를 가져올수 있다.)

                    //Response.Write(e.Row.Cells[2].Text);

                    //B. (출력과 상관없이) 바인딩되는 원본에 접근하는 방법

                    //전제 조건은 데이터 원본(쿼리문 실행된 값이 있어야 한다.) 화면상에 안보여도 상관없다.

                    //데이터원본(seq,subject,name, regDate, readCount, email)

                    //e.Row = 실제눈에보이는 게시판에 한줄 (출력된 행)

                    //그행을 만드는 사용된 실제 레코드이다.(데이터 원본 레코드)

                    DataRowView row = e.Row.DataItem as DataRowView;

                    //Response.Write(row["regDate"].ToString());

 

                    //3. 첨부파일 존재하면 이미지 추가하기

                    if (row["fileName"].ToString() != "")

                    {

                           LiteralControl img = new LiteralControl(string.Format("<img src='images/file.png' alt='{0}' title='{0}' style='margin-left:5px;' />", "다운로드:" + row["downloadCount"] + ""));

 

                           e.Row.Cells[1].Controls.Add(img);

                    }

 

                    //2. 글쓴지 일정시간이지나면.. New.gif 표시 30분 이내에 쓴글

                    //Response.Write(e.Row.Cells[3].Text + "<br />");

 

                   

                    DateTime regDate = (DateTime)row["regDate"];

 

                    //현재시간에서 글쓴시간 빼기

                    TimeSpan gap = DateTime.Now - regDate;

 

                    if (gap.TotalMinutes <= 30)

                    {

                           //30분 이내에 쓴글

                           //e.Row.Cells[1]

 

                           //타입을 맞추기위해 스트링 형태를 컨트롤 형태로 변환하기 위해 사용한다.

                           LiteralControl img = new LiteralControl("<img src='images/new.gif' alt='새글' style='margin-left:5px;' />");

                           e.Row.Cells[1].Controls.Add(img);

                           //e.Row.Cells[1].Controls.AddAt(0, img);

                    }

 

             }

            

 

       }

}

 


View.aspx

<%@ Page Title="" Language="C#" MasterPageFile="~/Board.master" AutoEventWireup="true" CodeFile="View.aspx.cs" Inherits="View" %>

 

<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">

</asp:Content>

<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">

       <table class="table" style="width:600px;">

             <tr>

                    <td align="center" bgcolor="Silver" width="150">

                           이름</td>

                    <td width="450">

                           &nbsp;&nbsp;<asp:Label ID="LabelName" runat="server"></asp:Label>

                    </td>

             </tr>

             <tr>

                    <td align="center" bgcolor="Silver" width="150">

                           이메일</td>

                    <td width="450">

                           &nbsp;&nbsp;<asp:Label ID="LabelEmail" runat="server"></asp:Label>

                    </td>

             </tr>

             <tr>

                    <td align="center" bgcolor="Silver" width="150">

                           제목</td>

                    <td width="450">

                           &nbsp;&nbsp;<asp:Label ID="LabelTitle" runat="server"></asp:Label>

                    </td>

             </tr>

             <tr>

                    <td align="center" bgcolor="Silver" width="150" style="padding:10px;">

                           내용</td>

                    <td width="450">

                           <asp:Label ID="LabelContent" runat="server"></asp:Label>

                    </td>

             </tr>

             <tr>

                    <td align="center" bgcolor="Silver" width="150">

                           번호</td>

                    <td width="450">

                           &nbsp;&nbsp;<asp:Label ID="LabelSeq" runat="server"></asp:Label>

                    </td>

             </tr>

             <tr>

                    <td align="center" bgcolor="Silver" width="150">

                           읽음</td>

                    <td width="450">

                           &nbsp;&nbsp;<asp:Label ID="LabelReadCount" runat="server"></asp:Label>

                    </td>

             </tr>

             <tr>

                    <td align="center" bgcolor="Silver" width="150">

                           날짜</td>

                    <td width="450">

                           &nbsp;&nbsp;<asp:Label ID="LabelDate" runat="server"></asp:Label>

                    </td>

             </tr>

             <tr>

                    <td align="center" bgcolor="Silver" width="150">

                           첨부파일</td>

                    <td width="450">

                           &nbsp;&nbsp;<asp:Label ID="LabelFile" runat="server" Text="LabelFile"></asp:Label>

                    </td>

             </tr>

 

       </table>

       <div style="text-align:center;margin:10px;">

             <asp:ImageButton ID="ImageButton1" runat="server" ImageUrl="~/images/list.gif"

                    onclick="ImageButton1_Click" ValidationGroup="list" style="width: 39px" />

             <asp:ImageButton ID="ImageButton2" runat="server"

                    ImageUrl="~/images/modify.gif" onclick="ImageButton2_Click"

                    style="width: 39px" />

             <asp:ImageButton ID="ImageButton3" runat="server"

                    ImageUrl="~/images/delete.gif" onclick="ImageButton3_Click" />

       </div>

</asp:Content>

 

View.cs

using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Web.UI;

using System.Web.UI.WebControls;

 

using System.Data;

using System.Data.SqlClient;

using System.Configuration;

using System.IO;

 

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

{

    protected void Page_Load(object sender, EventArgs e)

    {

             if (!IsPostBack)

             {

                    //seq의 상세보기

                    SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["conStr"].ConnectionString);

 

                    SqlCommand cmd = new SqlCommand();

                    cmd.Connection = con;

                    cmd.CommandText = "UPDATE Board SET readCount = readCount + 1 WHERE seq=@seq;";

 

                    //같은 번호이기 때문에 파라메터 하나만 던져도 가능하다.

                    cmd.Parameters.Add("@seq", SqlDbType.Int);

                    cmd.Parameters["@seq"].Value = Request.QueryString["seq"];

 

                    con.Open();

 

                    if (Request.Cookies["read"] == null || Request.Cookies["read"].Value == "n")

                    {

                           //조회수 증가

                           cmd.ExecuteNonQuery();

                           Response.Cookies["read"].Value = "y";

                    }

                   

 

                    cmd.CommandText = "select * from Board where seq=@seq";

 

 

                    //결과값으로 처리용도지만 ExecuteNonQuery 용도로도 사용할수는 있다.

                    SqlDataReader reader = cmd.ExecuteReader();

                    if (reader.Read())

                    {

                           //출력

                           LabelName.Text = string.Format("{0} [{1}]", reader["name"].ToString(), reader["userIP"].ToString());

                           LabelEmail.Text = reader["email"].ToString();

                           LabelTitle.Text = reader["subject"].ToString();

 

                           //글 내용

                           string content = reader["content"].ToString();

                          

                           //\r\n = 엔터값 출력(웹페이지) \r\n 출력 방식이 틀려서 엔터값이 안먹는다.<br />

                           //DB에는 \r\n으로 들어가 있다. 원본은 회손하지 않고 처리해야 한다.

 

                           //엔터값처리 -> <br />

                           content = content.Replace("\r\n", "<br />");

 

                           //첨부된 파일이 이미지라면.. 글내용에 첨부파일을 자동으로 게시

                           //첨부된 파일이 이미지라면.. 글내용에 첨부파일을 자동으로 게시

                           if (reader["fileName"].ToString() != "")

                           {

                                 if ((Path.GetExtension(reader["fileName"].ToString()).ToLower() == ".gif") || (Path.GetExtension(reader["fileName"].ToString()).ToLower() == ".jpg") || (Path.GetExtension(reader["fileName"].ToString()).ToLower() == ".jpeg") || (Path.GetExtension(reader["fileName"].ToString()).ToLower() == ".png"))

                                 {

                                        string img = string.Format("<div style='margin:20px;text-align:center'><img src='files/{0}' /></div>", reader["fileName"].ToString());

                                        content = img + content;

                                 }

                           }

 

                           //태그 적용 안함 처리 ( < - > &lt;)

                           if (reader["tag"].ToString() == "n")

                           {

                                 Server.HtmlEncode(content);

                           }

 

                           //<script> 무조건 적용안함

                           content = content.Replace("<script>", "&ltscript&gt").Replace("</script>", "&lt/script>");

                           //Response.Write(content);

 

                           LabelContent.Text = content;

                           LabelSeq.Text = reader["seq"].ToString();

                           LabelReadCount.Text = reader["readCount"].ToString();

                           LabelDate.Text = reader["regDate"].ToString();

                    }

 

                    //첨부파일 처리

                    if (reader["fileName"].ToString() != "")

                    {

                           LabelFile.Text = string.Format("<a href='Download.aspx?seq={2}&file={0}'>{0}</a> [다운로드횟수:{1}]", reader["fileName"].ToString(), reader["downloadCount"].ToString(), Request.QueryString["seq"]);

                    }

 

                    else

                    {

                           LabelFile.Text = "첨부파일 없음";

                    }

 

                    reader.Close();

                    con.Close();

             }

    }

       protected void ImageButton1_Click(object sender, ImageClickEventArgs e)

       {

             //List

             Response.Redirect("List.aspx");

       }

      

       protected void ImageButton3_Click(object sender, ImageClickEventArgs e)

       {

             //삭제 Del.aspx?seq=5

             Response.Redirect("Del.aspx?seq=" + Request.QueryString["seq"]);

       }

 

       protected void ImageButton2_Click(object sender, ImageClickEventArgs e)

       {

             //수정

             Response.Redirect("Edit.aspx?seq=" + Request.QueryString["seq"]);

       }

}

 



 


반응형