Exemplo n.º 1
0
    protected void btnSave_Click(object sender, EventArgs e)
    {
        if (Common.LoaiNguoiDungID() == 3 || Common.LoaiNguoiDungID() == 2)
        {
            QuangCao qcao = new QuangCao();
            if (Request.QueryString["id"] == null)
            {
                //Add new
                string path = Server.MapPath("../Upload/AdvImages");
                string relativePath = "";
                string randomString = "";
                string mediaType = "IMAGE";
                if (rbtFlash.Checked) mediaType = "FLASH";
                if (fileQuangCao.PostedFile.FileName != "")
                {
                    try
                    {
                        if ((fileQuangCao.PostedFile.ContentLength <= 300000) || (Common.LoaiNguoiDungID() == 3))
                        {
                            int pos = fileQuangCao.PostedFile.FileName.LastIndexOf('\\');
                            string absolutePath = path + "\\" + fileQuangCao.PostedFile.FileName.Remove(0, pos + 1);
                            if (File.Exists(absolutePath))
                            {
                                randomString = DateTime.Now.Ticks + "_";
                                absolutePath = path + "\\" + randomString +
                                               fileQuangCao.PostedFile.FileName.Remove(0, pos + 1);
                            }
                            fileQuangCao.PostedFile.SaveAs(absolutePath);
                            relativePath = "./Upload/AdvImages/" + randomString +
                                           fileQuangCao.PostedFile.FileName.Remove(0, pos + 1);
                        }
                        else
                        {
                            Response.Redirect("../message.aspx?msg=File size is bigger than 300KB!");
                            return;
                        }
                    }
                    catch (Exception ex)
                    {
                        Response.Redirect("../message.aspx?msg=" + ex.Message);
                        return;
                    }
                }
                qcao.InsertFields(txtDuongDan.Text, txtNoiDung.Text, relativePath, Common.NguoiDungID(), txtGhiChu.Text,
                                  mediaType, null, null, null, null, null);
            }
            else
            {
                //Edit
                int id = int.Parse(Request.QueryString["id"]);
                string path = Server.MapPath("../Upload/AdvImages");
                string randomString = "";
                string relativePath = "";
                string mediaType = "IMAGE";
                if (rbtFlash.Checked) mediaType = "FLASH";
                if (fileQuangCao.PostedFile.FileName != "")
                {
                    try
                    {
                        if ((fileQuangCao.PostedFile.ContentLength <= 300000) || (Common.LoaiNguoiDungID() == 3))
                        {
                            int pos = fileQuangCao.PostedFile.FileName.LastIndexOf('\\');
                            string absolutePath = path + "\\" + fileQuangCao.PostedFile.FileName.Remove(0, pos + 1);
                            if (File.Exists(absolutePath))
                            {
                                randomString = DateTime.Now.Ticks + "_";
                                absolutePath = path + "\\" + randomString +
                                               fileQuangCao.PostedFile.FileName.Remove(0, pos + 1);
                            }

                            if (!Directory.Exists(path))
                            {
                                Directory.CreateDirectory(path);
                            }
                            fileQuangCao.PostedFile.SaveAs(absolutePath);
                            //if (rbtFlash.Checked == true)
                            //{
                            //    fileQuangCao.PostedFile.SaveAs(absolutePath);
                            //}
                            //else
                            //{
                            //    Common.ResizeFromStream(absolutePath, 320, fileQuangCao.PostedFile.InputStream);
                            //}
                            //File.Delete(Server.MapPath(imgAnhQuangCao.Src));
                            relativePath = "./Upload/AdvImages/" + randomString +
                                           fileQuangCao.PostedFile.FileName.Remove(0, pos + 1);
                        }
                        else
                        {
                            Response.Redirect("../message.aspx?msg=File size is too big!");
                            return;
                        }
                    }
                    catch (Exception ex)
                    {
                        Response.Redirect("../message.aspx?msg=" + ex.Message);
                        return;
                    }
                }
                if (relativePath != "")
                {
                    qcao.UpdateFields(id, txtDuongDan.Text, txtNoiDung.Text, relativePath, null, txtGhiChu.Text,
                                      mediaType, null, null, null, null, null);
                }
                else
                {
                    qcao.UpdateFields(id, txtDuongDan.Text, txtNoiDung.Text, null, null, txtGhiChu.Text, mediaType, null,
                                      null, null, null, null);
                }
            }
            string strScript = "<script language='JavaScript'>" + "window.parent.Refresh();</script>";
            ClientScript.RegisterStartupScript(GetType(), "Refresh", strScript);
        }
        else
        {
            Response.Redirect("../message.aspx?msg=Access denied");
        }
    }