본문 바로가기

ASP.NET 기초 게시판/File_Upload&download_Board

파일업로드&다운로드 게시판_ Default2.aspx.cs

    protected void Page_Load(object sender, EventArgs e)
    {

        string deldate = Request.QueryString["date"];
        Label2.Text = deldate;
        
        if (Request.QueryString["idx"] == "hakdoc_")
        {
            Label3.Text = "학사모 문서파일";
        }
        else if (Request.QueryString["idx"] == "hakmail_")
        {
            Label3.Text = "학사모 메일";
        }
        else if (Request.QueryString["idx"] == "dondoc_")
        {
            Label3.Text = "돈자모 문서파일";
        }
        else
        {
            Label3.Text = "돈자모 메일";
        }
    }

     protected void Button1_Click(object sender, EventArgs e)
    {
        string type = Request.QueryString["idx"];
        string deldate = Request.QueryString["date"];
        int number = Convert.ToInt32(Request.QueryString["num"]);
        int typeNum = Convert.ToInt32(Request.QueryString["type"]);

        string extension;
        
        deldate = deldate.Replace("-", "");
        string FileUrl = Server.MapPath(ConfigurationManager.AppSettings["uploadPath"]) + type + deldate;

       
        if (FileUpload1.HasFile) ▶ 파일명이 업로드 컨드롤박스에 들어있는지
        {
            if (type == "hakdoc_" || type == "dondoc_")
            {
                extension = ".hwp";
            }
            else
            {
                extension = ".zip";
            }
            FileUrl += extension;
            string fileName = type + deldate + extension;

            String fileExtension = System.IO.Path.GetExtension(FileUpload1.FileName).ToLower();

            if (fileExtension == extension)
            {
                FileInfo fInfo = new FileInfo(FileUrl);
                if (fInfo.Exists)
                {
                    fInfo.Delete();
                }
                FileUpload1.SaveAs(FileUrl);
                display.Text = "업로드 파일 : " + FileUrl;
                Update_doubleo_board(number, typeNum, fileName);
            }
            else
            {
                display.Text = "파일의 확장자가 올바르지 않습니다";
            }

        }
    }

    public static void Update_doubleo_board(int idx, int type, string filename)
    {
        SqlConnection conn = CDBConn.GetConn();
        SqlCommand cmd = new SqlCommand();

        cmd.CommandText = "updateboard";
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.Connection = conn;

        cmd.Parameters.AddWithValue("@idx", idx);
        cmd.Parameters.AddWithValue("@type", type);
        cmd.Parameters.AddWithValue("@filename", filename);

        conn.Open();
        cmd.ExecuteNonQuery();
        conn.Close();
    }