コード例 #1
0
ファイル: ProfilePic.aspx.cs プロジェクト: 171107x/EADP
        protected void btnUpload_Click(object sender, EventArgs e)
        {
            string uploadFileName = "";
            string uploadFilePath = "";

            if (FU1.HasFile)
            {
                string ext = Path.GetExtension(FU1.FileName).ToLower();
                if (ext == ".jpg" || ext == ".jpeg" || ext == ".gif" || ext == ".png")
                {
                    uploadFileName = Guid.NewGuid().ToString() + ext;
                    uploadFilePath = Path.Combine(Server.MapPath("~/ProfilePic"), uploadFileName);
                    try
                    {
                        FU1.SaveAs(uploadFilePath);
                        imgUpload.ImageUrl = "~/ProfilePic/" + uploadFileName;
                        panCrop.Visible    = true;
                    }
                    catch (Exception ex)
                    {
                        lblMsg.Text = "Error! Please try again.";
                    }
                }
                else
                {
                    lblMsg.Text = "Selected file type not allowed!";
                }
            }
            else
            {
                lblMsg.Text = "Please select file first!";
            }
        }
コード例 #2
0
        protected void cargarImagen(object sender, EventArgs e)
        {
            string uploadFileName = "";
            string uploadFilePath = "";

            if (FU1.HasFile)
            {
                string ext = Path.GetExtension(FU1.FileName).ToLower();
                if (ext == ".jpg" || ext == ".jpeg" || ext == ".gif" || ext == ".png")
                {
                    uploadFileName = Guid.NewGuid().ToString() + ext;
                    uploadFilePath = Path.Combine(Server.MapPath("~/Huellas"), uploadFileName);
                    try
                    {
                        FU1.SaveAs(uploadFilePath);
                        imgUpload.ImageUrl = "~/Huellas/" + uploadFileName;
                    }
                    catch (Exception)
                    {
                        ClientScript.RegisterClientScriptBlock(this.GetType(), "alert", "<script type = 'text/javascript'> window.onload=function(){alert('Error, por favor trate de nuevo ')};</script>");
                    }
                }
                else
                {
                    ClientScript.RegisterClientScriptBlock(this.GetType(), "alert", "<script type = 'text/javascript'> window.onload=function(){alert('El tipo de archivo seleccionado no es válido')};</script>");
                }
            }
        }
コード例 #3
0
    protected void BtnFavicon_Click(object sender, EventArgs e)
    {
        if (FU1.HasFile)
        {
            try
            {
                if (FU1.PostedFile.ContentType != "image/x-icon")
                {
                    Snlg_Hata.ziyaretci.HataGosterHatali("Yanlızca ico dosyası yükleyebilirsiniz.", false);
                    return;
                }
                if (Path.GetExtension(FU1.FileName).ToLower() != ".ico")
                {
                    Snlg_Hata.ziyaretci.HataGosterHatali("Yanlızca .ico uzantılı dosyalar yükleyebilirsiniz.", false);
                    return;
                }

                FU1.SaveAs(Server.MapPath("~/uploads/images/favicon.ico"));
            }
            catch (Exception exc)
            {
                Snlg_Hata.ziyaretci.HataGosterHatali("Beklenmeyen bir hata oluştu. Hata : " + exc.Message, false);
                return;
            }
        }
    }
コード例 #4
0
    protected void btnProdSend_Click(object sender, EventArgs e)
    {
        Product pd = new Product()
        {
            Name   = txtProdName.Text,
            Price  = Convert.ToInt32(txtProdPrice.Text),
            Amount = Convert.ToInt32(txtProdAmount.Text),
        };

        if (FU1.HasFile)
        {
            string fileName = DateTime.Now.ToString("yyyyMMdd-hhmmss") + ".jpg";

            //1
            pd.ImageFileName = fileName;
            //2
            FU1.SaveAs(Server.MapPath("Upload/" + fileName));
        }

        ProductHandler pdh = new ProductHandler();

        pdh.AddToProducts(pd);

        this.Image1.ImageUrl = "~/Upload/" + FU1.FileName;
    }
コード例 #5
0
 protected void Button4_Click(object sender, EventArgs e)
 {
     {
         String filename = Path.GetFileName(FU1.FileName);
         FU1.SaveAs(Server.MapPath("~/Upload/") + filename);
         BLL obj = new BLL();
         BLL.Radio(DD1.SelectedItem.Value, FDate.Text.Trim(), TDate.Text.Trim(), DD2.SelectedItem.Value, TB1.Text.Trim(), DD3.SelectedItem.Value, DD4.SelectedItem.Value, RB1.Text.Trim(), TB2.Text.Trim(), filename);
     }
 }
コード例 #6
0
        protected void btnUpload_Click(object sender, EventArgs e)
        {
            // Upload Original Image Here
            string uploadFileName = "";
            string uploadFilePath = "";

            if (FU1.HasFile)
            {
                string ext = Path.GetExtension(FU1.FileName).ToLower();
                if (ext == ".jpg" || ext == ".jpeg" || ext == ".gif" || ext == ".png")
                {
                    uploadFileName = Guid.NewGuid().ToString() + ext;
                    uploadFilePath = Path.Combine(Server.MapPath("~/Fotos"), uploadFileName);
                    try
                    {
                        FU1.SaveAs(uploadFilePath);
                        imgUpload.ImageUrl      = "~/Fotos/" + uploadFileName;
                        panCrop.Visible         = true;
                        FU1.Visible             = false;
                        Label8.Visible          = false;
                        picture1.Visible        = false;
                        lblMsg.Visible          = false;
                        Image3.Visible          = false;
                        btnUpload.Visible       = false;
                        seleccionarfoto.Visible = false;
                        //ScriptManager.RegisterClientScriptBlock(this, GetType(), "mensaje", "photo();", true);
                    }
                    catch (Exception ex)
                    {
                        // lblMsg.Text = "Error!" + ex + ". Please try again.";
                        ScriptManager.RegisterClientScriptBlock(this, GetType(), "problem", "problema();", true);
                    }
                }
                else
                {
                    lblMsg.Text = "Esto no es una foto!";
                }
            }
            else
            {
                lblMsg.Text = "Seleccione una foto!";
            }
            string scriptventana = @"<script type='text/javascript'>
                                    $('#popup2').fadeIn('slow');
                                    $('#popup-overlay2').fadeIn('slow');
                                    </script>";

            ScriptManager.RegisterStartupScript(this, typeof(Page), "alerta", scriptventana, false);
            // ScriptManager.RegisterClientScriptBlock(this, GetType(), "ventana", scriptventana, true);
        }
コード例 #7
0
ファイル: ShopAdd.aspx.cs プロジェクト: xin1231/test
 protected void myUploadFile()  //連結資料庫
 {
     try
     {
         //SqlCommand myComm = new SqlCommand("", cnn);
         string folderPath = Server.MapPath("~/photo");    //取得絕對圖檔路徑
         //Check whether Directory (Folder) exists.
         if (!Directory.Exists(folderPath))
         {
             //If Directory (Folder) does not exists Create it.
             Directory.CreateDirectory(folderPath);
         }
         // 判斷 Server 上檔案名稱是否有重覆情況,有的話必須進行更名
         // 使用 Path.Combine 來集合路徑的優點
         //  以前發生過儲存 Table 內的是 \\ServerName\Dir(最後面沒有 \ 符號),
         //  直接跟 FileName 來進行結合,會變成 \\ServerName\DirFileName 的情況,
         //  資料夾路徑的最後面有沒有 \ 符號變成還需要判斷,但用 Path.Combine 來結合的話,
         //  資料夾路徑沒有 \ 符號,會自動補上,有的話,就直接結合
         string filename  = FU1.FileName;
         string extension = Path.GetExtension(filename).ToLowerInvariant();
         // 絶對路徑+檔名
         string serverFilePath = Path.Combine(folderPath, filename);
         string fileNameOnly   = Path.GetFileNameWithoutExtension(filename);
         int    fileCount      = 1;
         while (File.Exists(serverFilePath))
         {
             // 重覆檔案的命名規則為 檔名_1、檔名_2 以此類推
             filename       = string.Concat(fileNameOnly, "_", fileCount, extension);
             serverFilePath = Path.Combine(folderPath, filename);
             fileCount++;
         }
         //Save the File to the Directory (Folder).
         // FU1.SaveAs(folderPath + Path.GetFileName(FU1.FileName));
         FU1.SaveAs(serverFilePath);
         photoFileName = "photo/" + filename;
         //Display the Picture in Image control.
         imgPhoto.ImageUrl = photoFileName;
         //myComm.Parameters.AddWithValue("@tbSPhotoFileName", photoFileName);
         //myComm.ExecuteNonQuery();
     }
     catch (Exception ex)
     {
         Response.Write(ex.Message);
         // MessageBox.Show(this, ex.Message);
     }
 }
コード例 #8
0
    protected void Upload(object sender, EventArgs e)
    {
        //if (GridView1.Rows.Count > 0)
        //{
        //    Label7.Visible = true;
        //}

        // Upload Original Image Here
        string uploadFileName = "";
        string uploadFilePath = "";

        if (FU1.HasFile)
        {
            string ext = Path.GetExtension(FU1.FileName).ToLower();
            if (ext == ".jpg" || ext == ".jpeg" || ext == ".gif" || ext == ".png")
            {
                uploadFileName = Guid.NewGuid().ToString() + ext;
                uploadFilePath = Path.Combine(Server.MapPath("~/DynamicImage"), uploadFileName);
                try
                {
                    FU1.SaveAs(uploadFilePath);
                    Image2.ImageUrl = "~/DynamicImage/" + uploadFileName;
                }
                catch (Exception ex)
                {
                    //lblMsg.Text = ex.Message;
                }
            }
            else
            {
                //lblMsg.Text = "Selected file type not allowed!";
            }
        }
        else
        {
            //lblMsg.Text = "Save Successfully";
        }
    }
コード例 #9
0
    protected void LinkKaydet_Click(object sender, EventArgs e)
    {
        if (string.IsNullOrEmpty(Snlg_MemberInfo.s_UserId.ToString()) || string.IsNullOrEmpty(TxtEPost.Text.Trim()) || string.IsNullOrEmpty(TAd.Text.Trim()))
        {
            Snlg_Hata.ziyaretci.HataGosterHatali("Eposta ve isim girmelisiniz.", false, "#" + UserControlClientID);
            return;
        }

        if (!TSifre.Text.Equals(TSifre2.Text))
        {
            Snlg_Hata.ziyaretci.HataGosterHatali("İki şifre birbirinden farklı olamaz.", false, "#" + UserControlClientID);
            return;
        }

        #region parametreler
        Snlg_DBParameter[] DBPrms = new Snlg_DBParameter[5];
        try
        {
            DBPrms[0] = new Snlg_DBParameter("@UyeAd", SqlDbType.VarChar, TAd.Text);
            DBPrms[1] = new Snlg_DBParameter("@Eposta", SqlDbType.VarChar, TxtEPost.Text);
            if (TSifre.Text.ToString() != "")
            {
                DBPrms[2] = new Snlg_DBParameter("@Sifre", SqlDbType.VarChar, Snlg_ConfigValues.MD5Olustur(TSifre.Text));
            }
            else
            {
                DBPrms[2] = new Snlg_DBParameter("@Sifre", SqlDbType.VarChar, "");
            }
            DBPrms[3] = new Snlg_DBParameter("@Dil", SqlDbType.Int, Snlg_ConfigValues.s_ZDilId);
            DBPrms[4] = new Snlg_DBParameter("@UyeId", SqlDbType.Int, Snlg_MemberInfo.s_UserId);
        }
        catch
        {
            Snlg_Hata.ziyaretci.HataGosterHatali(Resources._default.ResourceManager.GetString("uyelik_asmx_hata10"), false, "#" + UserControlClientID);

            if (FU1.HasFile)
            {
                if (FU1.PostedFile.ContentType != "image/jpeg" && FU1.PostedFile.ContentType != "image/png" && FU1.PostedFile.ContentType != "image/x-png" && FU1.PostedFile.ContentType != "image/pjpeg" && FU1.PostedFile.ContentType != "image/gif")
                {
                    Snlg_Hata.ziyaretci.HataGosterHatali("Resim kaydedilmedi. Dosya türü uygun olmadığı için kabul edilmedi. Yanlızca JPG, JPEG, PNG ve GIF resimleri yükleyebilirsiniz.", false, "#" + UserControlClientID);
                }
                else if (FU1.PostedFile.ContentLength > 10485760)
                {
                    Snlg_Hata.ziyaretci.HataGosterHatali("Resim kaydedilmedi. Resmin boyutu 10 MB'dan büyük olduğu için resim kabuledilmedi.", true, "#" + UserControlClientID);
                }
                else
                {
                    string resimAdi = Resim_ismi_olustur(Path.GetExtension(FU1.FileName), "~/uploads/user/");
                    resimAdi = "/uploads/user/" + resimAdi;
                    FU1.SaveAs(Server.MapPath(resimAdi));
                    try { File.Delete(Server.MapPath(ImgRes.ImageUrl)); }
                    catch { }

                    DBPrms    = new Snlg_DBParameter[2];
                    DBPrms[0] = new Snlg_DBParameter("@UyeId", SqlDbType.Int, Snlg_MemberInfo.s_UserId);
                    DBPrms[1] = new Snlg_DBParameter("@Resim", SqlDbType.VarChar, resimAdi);
                    vt.SorguCalistir("UPDATE snlg_V1.TblUyeDetay SET Resim = @Resim FROM snlg_V1.TblUyeDetay AS TUD INNER JOIN snlg_V1.TblUyeApp AS TUA ON TUA.UyeId = TUD.UyeId  WHERE TUD.UyeId = @UyeId ", CommandType.Text, DBPrms);
                    ImgRes.ImageUrl = resimAdi;
                }
            }

            return;
        }


        #endregion

        try
        {
            vt.SorguCalistir("snlg_V1.zsp_UyeGuncelle", CommandType.StoredProcedure, DBPrms);
            Snlg_Hata.ziyaretci.HataGosterBasarili(Resources._default.ResourceManager.GetString("uyelik_asmx_hata05"), true, "#" + UserControlClientID);
            Response.Redirect(Request.RawUrl, false);
        }
        catch (Exception exc)
        {
            if (exc.Message.Contains("$#sqlhata1#$"))
            {
                Snlg_Hata.ziyaretci.HataGosterHatali(Resources._default.ResourceManager.GetString("uyelik_asmx_hata04"), false, "#" + UserControlClientID);
            }
            else if (exc.Message.Contains("$#sqlhata2#$"))
            {
                Snlg_Hata.ziyaretci.HataGosterHatali(Resources._default.ResourceManager.GetString("uyelik_asmx_hata03"), false, "#" + UserControlClientID);
            }
            else
            {
                Snlg_Hata.ziyaretci.ExceptionLogla(exc);
                Snlg_Hata.ziyaretci.HataGosterHatali(Resources._default.ResourceManager.GetString("hataBeklenmeyen") + exc.Message, false, "#" + UserControlClientID);
            }
        }
    }
コード例 #10
0
    protected void LinkZip_Click(object sender, EventArgs e)
    {
        if (!YetkiKontrol(pageName + "-Update"))
        {
            return;
        }


        try
        {
            string temaPath = Server.MapPath("~/uploads/temp/temp-uploaded");

            try { Directory.Delete(temaPath, true); }
            catch { }

            if (FU1.PostedFile.ContentType != "application/x-zip-compressed" && FU1.PostedFile.ContentType != "application/octet-stream" && FU1.PostedFile.ContentType != "application/zip" && FU1.PostedFile.ContentType != "application/x-rar-compressed")
            {
                Snlg_Hata.ziyaretci.HataGosterHatali(Resources.admin_language.theme_01, false);
                return;
            }
            if (Path.GetExtension(FU1.FileName).ToLower() != ".zip")
            {
                Snlg_Hata.ziyaretci.HataGosterHatali(Resources.admin_language.theme_02, false);
                return;
            }

            FU1.SaveAs(Server.MapPath("~/uploads/temp/temp-tema.zip"));
            FastZip fz = new FastZip();
            fz.ExtractZip(Server.MapPath("~/uploads/temp/temp-tema.zip"), temaPath, null);
            File.Delete(Server.MapPath("~/uploads/temp/temp-tema.zip"));

            bool          exist  = false;
            StringBuilder sbHtml = new StringBuilder();
            ListExistsFile(temaPath, sbHtml, 0, ref exist, temaPath);

            if (exist)
            {
                LtrList.Text = sbHtml.ToString();
                Snlg_Hata.ziyaretci.HataGosterHatali(Resources.admin_language.theme_03, false);
            }
            else
            {
                Snlg_Hata.ziyaretci.HataGosterBasarili(Resources.admin_language.theme_04, false);
            }

            if (Directory.Exists(Path.Combine(temaPath, "sql-scripts")))
            {
                string[] sqlFiles = Directory.GetFiles(Path.Combine(temaPath, "sql-scripts"), "*.sql");
                if (sqlFiles.Length > 0)
                {
                    Snlg_Hata.ziyaretci.HataGosterUyari(Resources.admin_language.theme_05.Replace("{0}", sqlFiles.Length.ToString()), false);
                }
                else
                {
                    Snlg_Hata.ziyaretci.HataGosterBasarili(Resources.admin_language.theme_06, false);
                }
            }
            else
            {
                Snlg_Hata.ziyaretci.HataGosterBasarili(Resources.admin_language.theme_07, false);
            }

            divButon.Visible = true;
        }
        catch (Exception exc)
        {
            Snlg_Hata.ziyaretci.HataGosterHatali(Resources.admin_language.theme_08 + exc.Message, false);
            return;
        }
    }
    protected void btnHide_Click(object sender, EventArgs e)
    {
        try
        {
            #region Code

            string CG = string.Empty;

            lblMsg.Text = "";

            if (Convert.ToInt16(hfGradeExam.Value) == 1)
            {
                CG = "CG";
                if (!string.IsNullOrEmpty(FU4.FileName))
                {
                    string examName = Path.GetFileNameWithoutExtension(FU4.FileName);
                    examName = examName.Substring(0, examName.IndexOf("_"));
                    CG      += "_" + examName;
                }
            }

            AlertSuccess.Visible = false;
            AlertWarning.Visible = false;
            if (FU1.HasFile)
            {
                spanFName1.InnerText = "";

                f1 = FU1;

                string NameOfFile = ValidateNameOfFile(f1); //Validate FileName

                string Tab = Path.GetFileNameWithoutExtension(f1.FileName);
                Tab = Tab.Substring(Tab.IndexOf("_"));
                Tab = Tab.Split('_')[1].Split('_')[0];
                Tab = Tab.Substring(3);

                if (Tab != "1")
                {
                    spanFName1.Attributes.Add("class", "text-danger");
                    spanFName1.InnerText = "File Should be _TAB1_" + NameOfFile;
                    return;
                }

                if (NameOfFile != "true")
                {
                    spanFName1.Attributes.Add("class", "text-danger");
                    spanFName1.InnerText = "File Should be .._TAB1_" + NameOfFile;//"File name does not match with selected Session & Year";
                    return;
                }

                string SessionOfFile = ValidateCategoryNameOfFile(f1);
                if (SessionOfFile != "true")
                {
                    lblMsg.Text = "You Have Not Selected Soft Data File According To Exam Category.";
                    lblMsg.Style.Add("color", "red");
                    FU1.Focus();
                    return;
                }

                StructUpload FUData = ConvertCSVtoDataTable(f1);
                FUData.TableName = "Tbl_" + ddlExamYear.SelectedItem.Text + "_" + ddlSession.SelectedItem.Text + "_" + CG + "_File1";
                //spanFName1.InnerText = FU1.FileName;

                #region Implement
                //string TableName = "Tbl_" + ddlExamYear.SelectedItem.Text + "_" + ddlSession.SelectedItem.Text + "_" + "_File1";
                //DataTable dtColumnCheck = dlFU.DtColumnCheck(TableName);
                //int i = 0;
                //foreach (DataRow dr in dtColumnCheck.Rows)
                //{
                //    foreach (DataColumn item in FUData.BulkDT.Columns)
                //    {
                //        if (dr[0].ToString() != item.ColumnName)
                //        {
                //            lblException.Text += "<br />" + item + " is not matching with " + dr[0];
                //        }
                //    }
                //    if (dr[i] == )
                //    {

                //    }
                //}
                #endregion

                listStruct.SetValue(FUData, 0);
                spanFName1.Attributes.Add("class", "text-primary");
                spanFName1.InnerText  = FUData.DataCount.ToString();
                spanFName1.InnerText += " - " + FU1.FileName;
            }
            else if (FU2.HasFile)
            {
                spanFName2.InnerText = "";

                f2 = FU2;

                string NameOfFile = ValidateNameOfFile(f2); //Validate FileName

                string Tab = Path.GetFileNameWithoutExtension(f2.FileName);
                Tab = Tab.Substring(Tab.IndexOf("_"));
                Tab = Tab.Split('_')[1].Split('_')[0];
                Tab = Tab.Substring(3);

                if (Tab != "2")
                {
                    spanFName2.Attributes.Add("class", "text-danger");
                    spanFName2.InnerText = "File Should be .._TAB2_" + NameOfFile;
                    return;
                }

                if (NameOfFile != "true")
                {
                    spanFName2.Attributes.Add("class", "text-danger");
                    spanFName2.InnerText = "File Should be .._TAB2_" + NameOfFile;
                    return;
                }

                string SessionOfFile = ValidateCategoryNameOfFile(f2);
                if (SessionOfFile != "true")
                {
                    lblMsg.Text = "You Have Not Selected Soft Data File According To Exam Category.";
                    lblMsg.Style.Add("color", "red");
                    FU2.Focus();
                    return;
                }

                StructUpload FUData = ConvertCSVtoDataTable(f2);
                FUData.TableName = "Tbl_" + ddlExamYear.SelectedItem.Text + "_" + ddlSession.SelectedItem.Text + "_" + CG + "_File2";
                listStruct.SetValue(FUData, 1);
                spanFName2.Attributes.Add("class", "text-primary");
                spanFName2.InnerText  = FUData.DataCount.ToString();
                spanFName2.InnerText += " - " + FU2.FileName;
            }
            else if (FU3.HasFile)
            {
                spanFName3.InnerText = "";

                f3 = FU3;

                string NameOfFile = ValidateNameOfFile(f3); //Validate FileName

                string Tab = Path.GetFileNameWithoutExtension(f3.FileName);
                Tab = Tab.Substring(Tab.IndexOf("_"));
                Tab = Tab.Split('_')[1].Split('_')[0];
                Tab = Tab.Substring(3);

                if (Tab != "3")
                {
                    spanFName3.Attributes.Add("class", "text-danger");
                    spanFName3.InnerText = "File Should be .._TAB3_" + NameOfFile;
                    return;
                }

                if (NameOfFile != "true")
                {
                    spanFName3.Attributes.Add("class", "text-danger");
                    spanFName3.InnerText = "File Should be .._TAB3_" + NameOfFile;
                    return;
                }

                string SessionOfFile = ValidateCategoryNameOfFile(f3);
                if (SessionOfFile != "true")
                {
                    lblMsg.Text = "You Have Not Selected Soft Data File According To Exam Category.";
                    lblMsg.Style.Add("color", "red");
                    FU3.Focus();
                    return;
                }

                StructUpload FUData = ConvertCSVtoDataTable(f3);
                FUData.TableName = "Tbl_" + ddlExamYear.SelectedItem.Text + "_" + ddlSession.SelectedItem.Text + "_" + CG + "_File3";
                listStruct.SetValue(FUData, 2);
                spanFName3.InnerText  = FUData.DataCount.ToString();
                spanFName3.InnerText += " - " + FU3.FileName;
                spanFName3.Attributes.Add("class", "text-primary");
            }
            else if (FU4.HasFile)
            {
                spanFName4.InnerText = "";

                f4 = FU4;

                string NameOfFile = ValidateNameOfFile(f4); //Validate FileName

                string Tab = Path.GetFileNameWithoutExtension(f4.FileName);
                Tab = Tab.Substring(Tab.IndexOf("_"));
                Tab = Tab.Split('_')[1].Split('_')[0];
                Tab = Tab.Substring(3);

                if (Tab != "4")
                {
                    spanFName4.Attributes.Add("class", "text-danger");
                    spanFName4.InnerText = "File Should be .._TAB4_" + NameOfFile;
                    return;
                }

                if (NameOfFile != "true")
                {
                    spanFName4.Attributes.Add("class", "text-danger");
                    spanFName4.InnerText = "File Should be .._TAB4_" + NameOfFile;
                    return;
                }

                string SessionOfFile = ValidateCategoryNameOfFile(f4);
                if (SessionOfFile != "true")
                {
                    lblMsg.Text = "You Have Not Selected Soft Data File According To Exam Category.";
                    lblMsg.Style.Add("color", "red");
                    FU4.Focus();
                    return;
                }

                StructUpload FUData = ConvertCSVtoDataTable(f4);
                FUData.TableName = "Tbl_" + ddlExamYear.SelectedItem.Text + "_" + ddlSession.SelectedItem.Text + "_" + CG + "_File4";
                listStruct.SetValue(FUData, 3);
                //listStruct.Insert(3, FUData);
                spanFName4.InnerText  = FUData.DataCount.ToString();
                spanFName4.InnerText += " - " + FU4.FileName;
                spanFName4.Attributes.Add("class", "text-primary");
            }
            else if (FU5.HasFile)
            {
                spanFName5.InnerText = "";

                f5 = FU5;

                string NameOfFile = ValidateNameOfFile(f5); //Validate FileName
                string Tab        = Path.GetFileNameWithoutExtension(f5.FileName);
                Tab = Tab.Substring(Tab.IndexOf("_"));
                Tab = Tab.Split('_')[1].Split('_')[0];
                Tab = Tab.Substring(3);

                if (Tab != "5")
                {
                    spanFName5.Attributes.Add("class", "text-danger");
                    spanFName5.InnerText = "File Should be .._TAB5_" + NameOfFile;
                    return;
                }

                if (NameOfFile != "true")
                {
                    spanFName5.Attributes.Add("class", "text-danger");
                    spanFName5.InnerText = "File Should be .._TAB5_" + NameOfFile;
                    return;
                }

                string SessionOfFile = ValidateCategoryNameOfFile(f5);
                if (SessionOfFile != "true")
                {
                    lblMsg.Text = "You Have Not Selected Soft Data File According To Exam Category.";
                    lblMsg.Style.Add("color", "red");
                    FU5.Focus();
                    return;
                }

                StructUpload FUData = ConvertCSVtoDataTable(f5);
                FUData.TableName = "Tbl_" + ddlExamYear.SelectedItem.Text + "_" + ddlSession.SelectedItem.Text + "_" + CG + "_File5";
                listStruct.SetValue(FUData, 4);
                //listStruct.Insert(4, FUData);
                spanFName5.InnerText  = FUData.DataCount.ToString();
                spanFName5.InnerText += " - " + FU5.FileName;
                spanFName5.Attributes.Add("class", "text-primary");
            }
            else if (FU6.HasFile)
            {
                spanFName6.InnerText = "";

                f6 = FU6;

                string NameOfFile = ValidateNameOfFile(f6); //Validate FileName
                string Tab        = Path.GetFileNameWithoutExtension(f6.FileName);
                Tab = Tab.Substring(Tab.IndexOf("_"));
                Tab = Tab.Split('_')[1].Split('_')[0];
                Tab = Tab.Substring(3);

                if (Tab != "6")
                {
                    spanFName6.Attributes.Add("class", "text-danger");
                    spanFName6.InnerText = "File Should be .._TAB6_" + NameOfFile;
                    return;
                }

                if (NameOfFile != "true")
                {
                    spanFName6.Attributes.Add("class", "text-danger");
                    spanFName6.InnerText = "File Should be .._TAB6_" + NameOfFile;
                    return;
                }

                string SessionOfFile = ValidateCategoryNameOfFile(f6);
                if (SessionOfFile != "true")
                {
                    lblMsg.Text = "You Have Not Selected Soft Data File According To Exam Category.";
                    lblMsg.Style.Add("color", "red");
                    FU6.Focus();
                    return;
                }

                StructUpload FUData = ConvertCSVtoDataTable(f6);
                FUData.TableName = "Tbl_" + ddlExamYear.SelectedItem.Text + "_" + ddlSession.SelectedItem.Text + "_" + CG + "_File6";
                listStruct.SetValue(FUData, 5);
                //listStruct.Insert(4, FUData);
                spanFName6.InnerText  = FUData.DataCount.ToString();
                spanFName6.InnerText += " - " + FU6.FileName;
                spanFName6.Attributes.Add("class", "text-primary");
            }
            else
            {
                lblException.Text += "File is empty please check!";
                Page.ClientScript.RegisterStartupScript(this.GetType(), "ShowException", "ShowException()", true);
            }
            #endregion
        }
        catch (Exception ex)
        {
            lblException.Text += ex.Message;
            Page.ClientScript.RegisterStartupScript(this.GetType(), "ShowException", "ShowException()", true);
        }
        finally
        {
            if (listStruct != null)
            {
                if (!string.IsNullOrEmpty(listStruct[0].FileName) && !string.IsNullOrEmpty(listStruct[1].FileName) && !string.IsNullOrEmpty(listStruct[2].FileName) && !string.IsNullOrEmpty(listStruct[3].FileName) && !string.IsNullOrEmpty(listStruct[4].FileName))
                {
                    if (Convert.ToInt16(hfGradeExam.Value) == 1)
                    {
                        if (!string.IsNullOrEmpty(listStruct[5].FileName))
                        {
                            btnSave.Visible = true;
                        }
                    }
                    else
                    {
                        btnSave.Visible = true;
                    }
                }
            }
            else
            {
                listStruct           = new StructUpload[6];
                spanFName1.InnerText = spanFName2.InnerText = spanFName3.InnerText = spanFName4.InnerText = spanFName5.InnerText = spanFName6.InnerText = "";
                Page.ClientScript.RegisterStartupScript(this.GetType(), "Alert", "alert('All files has empty. please file choose again')", true);
            }
            //Page.ClientScript.RegisterStartupScript(this.GetType(), "ShowConfirmation", "ShowConfirmation()", true);
        }
    }
コード例 #12
0
        protected void Button1_Click1(object sender, EventArgs e)
        {
            Boolean fileOk   = false;
            String  savePath = DAL.SystemSet.savePath(Session["DatabaseName"].ToString());

            if (FU1.HasFile == false)
            {
                return;
            }

            // FU1.FileName 只有 "檔案名稱.附檔名",並沒有 Client 端的完整理路徑
            string filename = FU1.FileName;

            string extension = Path.GetExtension(filename).ToLowerInvariant();
            // 判斷是否為允許上傳的檔案附檔名

            String fileExtension = System.IO.Path.GetExtension(FU1.FileName).ToLower();  //取得上傳的檔案類型


            //檢查檔案大小

            string[] allowExtension = { ".jpg", ".gif", ".png", ".jpeg" };
            //对上传的文件的类型进行一个个匹对
            for (int i = 0; i < allowExtension.Length; i++)
            {
                if (fileExtension == allowExtension[i])
                {
                    fileOk = true;
                    break;
                }
            }
            //
            if (fileOk)
            {
                ScriptManager.RegisterStartupScript((System.Web.UI.Page)HttpContext.Current.Handler, this.GetType(), "ShowMessage", "alert('只允許上傳圖片');", true);
            }

            // 限制檔案大小,限制為 2MB

            /**
             * int filesize = FU1.PostedFile.ContentLength;
             * if (filesize > 2100000)
             * {
             *  lblMessage.Text = "檔案大小上限為 2MB,該檔案無法上傳";
             *  return;
             * }
             **/
            // 檢查 Server 上該資料夾是否存在,不存在就自動建立
            //string serverDir = @"G:\FileUploadDemo";
            if (Directory.Exists(savePath) == false)
            {
                Directory.CreateDirectory(savePath);
            }

            // 判斷 Server 上檔案名稱是否有重覆情況,有的話必須進行更名
            // 使用 Path.Combine 來集合路徑的優點
            //  以前發生過儲存 Table 內的是 \\ServerName\Dir(最後面沒有 \ 符號),
            //  直接跟 FileName 來進行結合,會變成 \\ServerName\DirFileName 的情況,
            //  資料夾路徑的最後面有沒有 \ 符號變成還需要判斷,但用 Path.Combine 來結合的話,
            //  資料夾路徑沒有 \ 符號,會自動補上,有的話,就直接結合
            string serverFilePath = Path.Combine(savePath, filename);
            string fileNameOnly   = Path.GetFileNameWithoutExtension(filename);
            int    fileCount      = 1;

            while (File.Exists(serverFilePath))
            {
                // 重覆檔案的命名規則為 檔名_1、檔名_2 以此類推
                filename       = string.Concat(fileNameOnly, "_", fileCount, extension);
                serverFilePath = Path.Combine(savePath, filename);
                fileCount++;
            }



            // 把檔案傳入指定的 Server 內路徑
            // try
            //{
            if (fileOk)
            {
                String        DBName  = Session["DatabaseName"].ToString();
                SqlConnection connStr = new SqlConnection(Utility.DBconnection.connect_string(DBName));

                connStr.Open();                                                                                                                                                                                                                                                    //開啟資料庫的連結

                String     select = "INSERT INTO KCheckListLostUpload(KCheckListLostId,DateTime,UploadAddress,Remark,Status) Values('" + Label1.Text + "','" + DateTime.Now.ToString("yyyy/MM/dd") + "','" + filename + "','" + TextBox1.Text + "','" + DropDownList1.Text + "')"; //宣告SQL語法的字串,這邊可依照自行需求修改
                SqlCommand cmd    = new SqlCommand(select, connStr);
                cmd.ExecuteNonQuery();


                string tempName = tempPath + FU1.FileName;
                //string imageName = savePath + FU1.FileName;

                // 儲存暫存檔
                FU1.SaveAs(tempName);

                // System.Web.UI.WebControls 與 System.Drawing 同時擁有 Image 類別
                // 所以以下程式碼明確指定要使用的是 System.Drawing.Image

                System.Drawing.Image.GetThumbnailImageAbort callBack =
                    new System.Drawing.Image.GetThumbnailImageAbort(ThumbnailCallback);
                Bitmap image = new Bitmap(tempName);

                // 計算維持比例的縮圖大小
                int[] thumbnailScale = getThumbnailImageScale(300, 300, image.Width, image.Height);

                // 產生縮圖
                System.Drawing.Image smallImage =
                    image.GetThumbnailImage(thumbnailScale[0], thumbnailScale[1], callBack, IntPtr.Zero);

                // 將縮圖存檔
                smallImage.Save(serverFilePath);

                // 釋放並刪除暫存檔
                image.Dispose();
                System.IO.File.Delete(tempName);

                //FU1.SaveAs(serverFilePath);
                lblMessage.Text = "檔案上傳成功";

                ScriptManager.RegisterStartupScript((System.Web.UI.Page)HttpContext.Current.Handler, this.GetType(), "ShowMessage", "alert('檔案上傳成功');", true);
            }
            // }
            // catch (Exception ex)
            //  {
            //     lblMessage.Text = ex.Message;
            // }
        }