using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.IO;
namespace SampleCS.Upload
{
/// <summary>
/// Write에 대한 요약 설명입니다.
/// </summary>
public class Write : System.Web.UI.Page
{
protected System.Web.UI.WebControls.TextBox txtName;
protected System.Web.UI.WebControls.TextBox txtEmail;
protected System.Web.UI.WebControls.RegularExpressionValidator RegularExpressionValidator1;
protected System.Web.UI.WebControls.TextBox txtTitle;
protected System.Web.UI.WebControls.RequiredFieldValidator Requiredfieldvalidator2;
protected System.Web.UI.WebControls.TextBox txtContent;
protected System.Web.UI.WebControls.RequiredFieldValidator RequiredFieldValidator3;
protected System.Web.UI.WebControls.TextBox txtHomepage;
protected System.Web.UI.WebControls.RegularExpressionValidator RegularExpressionValidator2;
protected System.Web.UI.WebControls.RequiredFieldValidator RequiredFieldValidator1;
protected System.Web.UI.WebControls.RequiredFieldValidator Requiredfieldvalidator5;
protected System.Web.UI.WebControls.RadioButtonList rdoEncoding;
protected System.Web.UI.WebControls.Button btnWrite;
protected System.Web.UI.WebControls.TextBox txtPassword;
protected System.Web.UI.WebControls.ValidationSummary ValidationSummary1;
protected System.Web.UI.WebControls.Button btnList;
protected System.Web.UI.HtmlControls.HtmlInputFile fileUpload;
protected System.Web.UI.WebControls.CheckBox chkUpload;
protected System.Web.UI.WebControls.Panel Panel1;
protected string strBaseDir;
protected string strFileName;
protected int intFileSize;
private void Page_Load(object sender, System.EventArgs e)
{
// 여기에 사용자 코드를 배치하여 페이지를 초기화합니다.
}
#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 이 호출은 ASP.NET Web Form 디자이너에 필요합니다.
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// 디자이너 지원에 필요한 메서드입니다.
/// 이 메서드의 내용을 코드 편집기로 수정하지 마십시오.
/// </summary>
private void InitializeComponent()
{
this.chkUpload.CheckedChanged += new System.EventHandler(this.chkUpload_CheckedChanged);
this.btnWrite.Click += new System.EventHandler(this.btnWrite_Click);
this.btnList.Click += new System.EventHandler(this.btnList_Click);
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
private void btnWrite_Click(object sender, System.EventArgs e)
{
SqlConnection objCon = new SqlConnection();
objCon.ConnectionString = Application["CONNECTION_STRING"].ToString();
objCon.Open();
string strName = txtName.Text.Replace("&", "&").Replace("<", "<").Replace(">", ">");
string strTitle = txtTitle.Text.Replace("&", "&").Replace("<", "<").Replace(">", ">");
//strBaseDir = Server.MapPath("/") + "\\Files\\Upload\\";
string strBaseDir = //저장될 폴더
//@"d:\inetpub\wwwroot\web\uploadcs\files";
Server.MapPath(".") + "\\files";
strFileName = "";
intFileSize = 0;
if(fileUpload.PostedFile != null)
{
if(fileUpload.PostedFile.FileName.Trim().Length > 0 && fileUpload.PostedFile.ContentLength > 0)
{
strFileName = Path.GetFileName(fileUpload.PostedFile.FileName);
intFileSize = fileUpload.PostedFile.ContentLength;
//업로드
fileUpload.PostedFile.SaveAs(GetUinqueFileNameWithPath(strBaseDir, strFileName));
}
}
SqlCommand objCmd = new SqlCommand();
objCmd.Connection = objCon;
objCmd.CommandText = "procWriteUpload";
objCmd.Parameters.Add("@Name", SqlDbType.VarChar, 25);
objCmd.Parameters.Add("@Email", SqlDbType.VarChar, 100);
objCmd.Parameters.Add("@Title", SqlDbType.VarChar, 150);
objCmd.Parameters.Add("@PostIP", SqlDbType.VarChar, 15);
objCmd.Parameters.Add("@Content", SqlDbType.Text);
objCmd.Parameters.Add("@Password", SqlDbType.VarChar, 20);
objCmd.Parameters.Add("@Encoding", SqlDbType.VarChar, 10);
objCmd.Parameters.Add("@Homepage", SqlDbType.VarChar, 100);
objCmd.Parameters.Add("@FileName", SqlDbType.VarChar, 255);
objCmd.Parameters.Add("@FileSize", SqlDbType.Int);
objCmd.Parameters["@Name"].Value = strName;
objCmd.Parameters["@Email"].Value = strTitle;
objCmd.Parameters["@Title"].Value = txtTitle.Text;
objCmd.Parameters["@PostIP"].Value = Request.UserHostAddress;
objCmd.Parameters["@Content"].Value = txtContent.Text.Replace("<xmp>", "");
objCmd.Parameters["@Password"].Value = txtPassword.Text;
objCmd.Parameters["@Encoding"].Value = rdoEncoding.SelectedItem.Text;
objCmd.Parameters["@Homepage"].Value = txtHomepage.Text;
objCmd.Parameters["@FileName"].Value = strFileName;
objCmd.Parameters["@FileSize"].Value = intFileSize;
objCmd.CommandType = CommandType.StoredProcedure;
objCmd.ExecuteNonQuery();
objCon.Close();
Response.Redirect("./List.aspx");
}
private void btnList_Click(object sender, System.EventArgs e)
{
Response.Redirect("./List.aspx");
}
private void chkUpload_CheckedChanged(object sender, System.EventArgs e)
{
if(chkUpload.Checked)
{
Panel1.Visible = true;
}
else
{
Panel1.Visible = false;
}
}
// 파일명 뒤에 번호 붙이는 메서드
private string GetUinqueFileNameWithPath(string strBaseDirTemp, string strFileNameTemp)
{
int indexOfDot = strFileNameTemp.LastIndexOf(".");
string strName = strFileNameTemp.Substring(0, indexOfDot);
string strExt = strFileNameTemp.Substring(indexOfDot+1);
bool blnExists = true;
int i = 0;
while(blnExists)
{
if(File.Exists(strBaseDirTemp + strFileNameTemp))
{
i++;
strFileNameTemp = strName + "(" + i + ")." + strExt;
}
else
{
blnExists = false;
}
}
strFileName = strFileNameTemp;
return Path.Combine(strBaseDirTemp, strFileNameTemp);
}
}
}