1 using System;
 2
using System;
 2 using System.IO;
 3
using System.IO;
 3 using Microsoft.Practices.EnterpriseLibrary.Data;
 4
using Microsoft.Practices.EnterpriseLibrary.Data;
 4 using System.Data;
 5
using System.Data;
 5 6
 6 public partial class Photo_PhotoAddControl : System.Web.UI.UserControl
 7
public partial class Photo_PhotoAddControl : System.Web.UI.UserControl
 7
 ...{
 8
...{
 8 protected void Page_Load(object sender, EventArgs e)
 9
    protected void Page_Load(object sender, EventArgs e)
 9
 ...{
10
    ...{
10 // Empty
11
        // Empty
11 }
12
    }
12
 protected void btnAdd_Click(object sender, EventArgs e) ...{
13
    protected void btnAdd_Click(object sender, EventArgs e) ...{
13 // 파일첨부 확인
14
        // 파일첨부 확인
14
 if (ctlFileName.PostedFile.ContentLength > 0) ...{
15
        if (ctlFileName.PostedFile.ContentLength > 0) ...{
15 // 파일 용량 제한
16
            // 파일 용량 제한
16
 if (ctlFileName.PostedFile.ContentLength <= (1048576 * 4)) ...{
17
            if (ctlFileName.PostedFile.ContentLength <= (1048576 * 4)) ...{
17 // 파일 확장자 검사 : 이미지 파일만 업로드
18
                // 파일 확장자 검사 : 이미지 파일만 업로드
18 string ext = Path.GetExtension(ctlFileName.FileName);
19
                string ext = Path.GetExtension(ctlFileName.FileName);
19 if (ext == ".gif" || ext == ".jpg" 
20
                if (ext == ".gif" || ext == ".jpg" 
20
 || ext == ".jpeg" || ext == ".png") ...{
21
                    || ext == ".jpeg" || ext == ".png") ...{
21 //[1] 중복파일 제거 후 사진 업로드
22
                    //[1] 중복파일 제거 후 사진 업로드
22
 파일업로드/파일명 중복 처리 후 업로드#region 파일업로드/파일명 중복 처리 후 업로드
23
                    파일업로드/파일명 중복 처리 후 업로드#region 파일업로드/파일명 중복 처리 후 업로드
23 // 파일 업로드
24
    // 파일 업로드
24 // strDirectory : 같은 경로의 files에 저장하겠다는 의미
25
    // strDirectory : 같은 경로의 files에 저장하겠다는 의미
25 string strDirectory = Server.MapPath(".") + "\\files\\";
26
    string strDirectory = Server.MapPath(".") + "\\files\\";
26 // strFileName은 넘겨온 파일명으로 업로드 파일명 결정
27
    // strFileName은 넘겨온 파일명으로 업로드 파일명 결정
27 // 추가적으로 이미 업로드된 파일명이 있으면 뒤에 번호 붙임
28
    // 추가적으로 이미 업로드된 파일명이 있으면 뒤에 번호 붙임
28 string strFileName = String.Empty;
29
    string strFileName = String.Empty;
29 // 넘겨온 파일명이 있다면, 업로드
30
    // 넘겨온 파일명이 있다면, 업로드
30 if (!String.IsNullOrEmpty(ctlFileName.FileName))
31
    if (!String.IsNullOrEmpty(ctlFileName.FileName))
31
 ...{
32
    ...{
32 strFileName = GetFilePath(strDirectory, ctlFileName.FileName); // 파일명 추출
33
        strFileName = GetFilePath(strDirectory, ctlFileName.FileName); // 파일명 추출
33 ctlFileName.PostedFile.SaveAs(
34
        ctlFileName.PostedFile.SaveAs(
34 Path.Combine(strDirectory, strFileName)); // 경로 및 파일명으로 저장 실행
35
            Path.Combine(strDirectory, strFileName)); // 경로 및 파일명으로 저장 실행
35 }
36
    }
36 #endregion
37
                    #endregion
37 //[2] DB에 데이터 저장 
38
                    //[2] DB에 데이터 저장 
38 string sql = String.Format(@"
39
                    string sql = String.Format(@"
39 Insert Into Photos
40
Insert Into Photos
40 (Name, Title, Content, PostIP, Password, Category, FileName, FileSize)
41
    (Name, Title, Content, PostIP, Password, Category, FileName, FileSize)
41 Values
42
Values
42 ('{0}', '{1}', '{2}', '{3}', '{4}', '{5}', '{6}', '{7}')"
43
    ('{0}', '{1}', '{2}', '{3}', '{4}', '{5}', '{6}', '{7}')"
43 , txtName.Text  
44
                        , txtName.Text  
44 , txtTitle.Text
45
                        , txtTitle.Text
45 , txtContent.Text
46
                        , txtContent.Text
46 , Request.UserHostAddress
47
                        , Request.UserHostAddress
47 , txtPassword.Text
48
                        , txtPassword.Text
48 , lstCategory.SelectedValue
49
                        , lstCategory.SelectedValue
49 , strFileName
50
                        , strFileName
50 , ctlFileName.PostedFile.ContentLength
51
                        , ctlFileName.PostedFile.ContentLength
51 );
52
                        );
52 DatabaseFactory.CreateDatabase("ConnectionString")
53
                    DatabaseFactory.CreateDatabase("ConnectionString")
53 .ExecuteNonQuery(CommandType.Text, sql); // DB저장
54
                        .ExecuteNonQuery(CommandType.Text, sql); // DB저장
54 Response.Redirect("PhotoList.aspx"); // 이동
55
                    Response.Redirect("PhotoList.aspx"); // 이동
55 }
56
                }
56
 else ...{ lblError.Text = "이미지 파일만 업로드하세요."; }
57
                else ...{ lblError.Text = "이미지 파일만 업로드하세요."; }
57 }
58
            }
58
 else ...{ // 4096 Kbytes 이상 자료를 업로드 하고자한다면,
59
            else ...{ // 4096 Kbytes 이상 자료를 업로드 하고자한다면,
59 // http://support.microsoft.com/kb/815307/ko
60
                // http://support.microsoft.com/kb/815307/ko
60 lblError.Text = "4MB 이하만 업로드 가능합니다.";
61
                lblError.Text = "4MB 이하만 업로드 가능합니다.";
61 }
62
            }
62 }
63
        }
63
 else ...{ lblError.Text = "사진을 첨부하셔야 합니다."; }
64
        else ...{ lblError.Text = "사진을 첨부하셔야 합니다."; }
64 }
65
    }
65 66
66
 중복된 파일명 뒤에 번호 붙이는 메서드 : GetFilePath#region 중복된 파일명 뒤에 번호 붙이는 메서드 : GetFilePath
67
    중복된 파일명 뒤에 번호 붙이는 메서드 : GetFilePath#region 중복된 파일명 뒤에 번호 붙이는 메서드 : GetFilePath
67
 /**//// <summary>
68
    /**//// <summary>
68 /// GetFilePath : 파일명 뒤에 번호 붙이는 메서드
69
    /// GetFilePath : 파일명 뒤에 번호 붙이는 메서드
69 /// </summary>
70
    /// </summary>
70 /// <param name="strBaseDirTemp">경로(c:\MyFiles)</param>
71
    /// <param name="strBaseDirTemp">경로(c:\MyFiles)</param>
71 /// <param name="strFileNameTemp">Test.txt</param>
72
    /// <param name="strFileNameTemp">Test.txt</param>
72 /// <returns>Test(1).txt</returns>
73
    /// <returns>Test(1).txt</returns>
73 private string GetFilePath(
74
    private string GetFilePath(
74 string strBaseDirTemp, string strFileNameTemp)
75
      string strBaseDirTemp, string strFileNameTemp)
75
 ...{
76
    ...{
76 string strName = //순수파일명 : Test
77
        string strName = //순수파일명 : Test
77 Path.GetFileNameWithoutExtension(strFileNameTemp);
78
            Path.GetFileNameWithoutExtension(strFileNameTemp);
78 string strExt =        //확장자 : .txt
79
        string strExt =        //확장자 : .txt
79 Path.GetExtension(strFileNameTemp);
80
            Path.GetExtension(strFileNameTemp);
80 bool blnExists = true;
81
        bool blnExists = true;
81 int i = 0;
82
        int i = 0;
82 while (blnExists)
83
        while (blnExists)
83
 ...{
84
        ...{
84 //Path.Combine(경로, 파일명) = 경로+파일명
85
            //Path.Combine(경로, 파일명) = 경로+파일명
85 if (File.Exists(Path.Combine(strBaseDirTemp, strFileNameTemp)))
86
            if (File.Exists(Path.Combine(strBaseDirTemp, strFileNameTemp)))
86
 ...{
87
            ...{
87 strFileNameTemp =
88
                strFileNameTemp =
88 strName + "(" + ++i + ")" + strExt;//Test(3).txt
89
                  strName + "(" + ++i + ")" + strExt;//Test(3).txt
89 }
90
            }
90 else
91
            else
91
 ...{
92
            ...{
92 blnExists = false;
93
                blnExists = false;
93 }
94
            }
94 }
95
        }
95 return strFileNameTemp;
96
        return strFileNameTemp;
96 }
97
    }
97 #endregion
98
    #endregion
98 }
99
}
99