//############################################# protected void btn_Save_Click(object sender, EventArgs e) { if (Page.IsValid) { if (fileUploader.HasFile) { string QExt = "pdf"; string AttExt = "jpg"; string fullPathQuestionFile = ""; string fullPathAttachFile = ""; string fullPathZipFile = ""; //helper class to transfer data to db var Q_Att_Dto = new ExamQuestionDto(); ViewState["Qs_Attach"] = null; if (AttachUploader.HasFile) { AttExt = CommonBusiness.getFileExtension(AttachUploader.FileName).ToLower(); } if ((QExt != "pdf" && QExt != "jpg") || (AttExt != "pdf" && AttExt != "jpg")) { rwm.RadAlert("فرمت صحیح انتخاب گردد", null, 100, "خطا", ""); } else { string did = Request.QueryString["did"].ToString(); var examQ = eBusiness.GetExamQuestionsbyDid(did, null, null); if (examQ != null && bool.Parse(examQ.Rows[0]["TemplateDownloaded"].ToString()) == true) { var userID = Session[sessionNames.userID_StudentOstad].ToString(); var questionID = examQ.AsEnumerable().Select(s => s.Field <int>("examQuestionID")).FirstOrDefault(); var relativePath = $"~/QueizPapers/{examQ.Rows[0]["Term"].ToString()}/{userID}/pdfFiles/{did}"; string absolutePath = Server.MapPath(relativePath); fullPathZipFile = $"{absolutePath}/{did}.zip"; string filepass = generaterandomstr(8); //Directory.GetDirectories(absolutePath).ToList().ForEach(dir => Directory.Delete(dir, true)); //Directory.GetFiles(absolutePath, "*.*").Where(w=>w.Contains("_canceled")).ToList().ForEach(file => File.Delete(file)); if (fileUploader.HasFile)//آپلود سوالات { Q_Att_Dto.IsExistQuestionFile = true; Q_Att_Dto.Did = did.ToString(); Q_Att_Dto.QuestionId = questionID.ToString(); Q_Att_Dto.UserId = userID; Q_Att_Dto.HashedPass = EncryptionClass.EncryptRJ(filepass); Q_Att_Dto.RelativePath = relativePath; QExt = CommonBusiness.getFileExtension(fileUploader.FileName).ToLower(); if (!Directory.Exists(absolutePath)) { Directory.CreateDirectory(absolutePath); } fullPathQuestionFile = $"{absolutePath}/{did.ToString()}.{QExt}"; if (QExt == "pdf" || QExt == "jpg") { //var file = fileUploader.PostedFile; var qfSaved = SaveFile(fileUploader.PostedFile, absolutePath, did.ToString() + "." + QExt); if (!qfSaved) { rwm.RadAlert("خطا در روند بارگذاری فایل سوالات! لطفا مجدداً بارگذاری نمائید.", null, 100, "", ""); return; } using (ZipFile zip = new ZipFile()) { zip.Password = filepass; FileStream stream = null; using (stream = new FileStream(fullPathQuestionFile, FileMode.Open, FileAccess.ReadWrite)) { zip.AddEntry(did.ToString() + "." + QExt, stream); zip.Save(fullPathZipFile); } } } } if (AttachUploader.HasFile)//آپلود سوالت پیوست { Q_Att_Dto.IsExistAttachFile = true; AttExt = CommonBusiness.getFileExtension(AttachUploader.FileName).ToLower(); fullPathAttachFile = $"{absolutePath}/{did.ToString()}Attached.{AttExt}"; if (AttExt == "jpg" || AttExt == "pdf") { Q_Att_Dto.AttExt = AttExt; var att_fSaved = SaveAttachFile(AttachUploader.PostedFile, absolutePath, did.ToString() + "Attached." + AttExt); if (!att_fSaved) { rwm.RadAlert("خطا در روند بارگذاری فایل پیوست سوالات! لطفا مجدداً بارگذاری نمائید.", null, 100, "", ""); return; } using (ZipFile zip = new ZipFile(fullPathZipFile)) { zip.Password = filepass; FileStream stream = null; stream = new FileStream(fullPathAttachFile, FileMode.Open, FileAccess.ReadWrite); zip.AddEntry(did.ToString() + "Attached." + AttExt, stream); zip.Save(fullPathZipFile); } } } Directory.GetFiles(absolutePath, "*.*").Where(w => !w.Contains(".zip")).ToList().ForEach(file => File.Delete(file)); if (File.Exists(fullPathZipFile)) { var whiteTape = Server.MapPath("~/University/Theme/images/whitePaper.jpg"); var questioHeaderTemplate = Server.MapPath("~/University/Theme/images/QuestionHeaderTemplate.jpg"); var examQ_Detail = eBusiness.Get_ExamdetailbyDid(did, null, null); var constQuestionFileInfo = examQ_Detail.AsEnumerable().Select(x => new ExamStudentDTO() { TypeNimsal = x.Field <string>("typeNimsal"), CourseTitle = x.Field <string>("namedars"), ProfossorFullName = x.Field <string>("osname"), ExamDate = x.Field <string>("dateexam"), ExamTime = x.Field <string>("saatexam"), KeyCode = x.Field <string>("keyCode"), ExamDuration = x.Field <string>("examTime"), Calculator = x.Field <string>("calculator"), Note = x.Field <string>("note"), LowBook = x.Field <string>("LowBook"), ClassCode = x.Field <string>("ClassCode").ToString(), Grade = x.Field <string>("magh").ToString(), Major = x.Field <string>("nameresh").ToString() }).FirstOrDefault(); eBusiness.ChangeTemplateOfQuestion(absolutePath, did.ToString(), filepass, constQuestionFileInfo, questioHeaderTemplate, whiteTape, userID); pnlMainQuestion_Att_Uploaded.Visible = false; pnl_GeneratedPdf_MergedQuestion_Att.Visible = true; var lastPdfPath = $"{Request.Url.GetLeftPart(UriPartial.Authority)}/{relativePath.Replace("~/", "")}/{userID}/{did}_Momtahen_{userID}_1.pdf?ts={DateTime.Now.Ticks}"; PdfFileSource = lastPdfPath; ViewState["Qs_Attach"] = Q_Att_Dto; //var buff = System.IO.File.ReadAllBytes(asbPathMerged_Q_Att); //string base64String = Convert.ToBase64String(buff, 0, buff.Length); //SrcMergedFile = $"data:{PdfMimeType};base64,{base64String}"; //string scrp = "function f(){showLightBox(); $find(\"" +.ClientID + "\").show(); Sys.Application.remove_load(f);}Sys.Application.add_load(f);"; //ScriptManager.RegisterStartupScript(this.Page, GetType(), ClientID, scrp, true); //ContentPlaceHolder mpContentPlaceHolder ; //mpContentPlaceHolder = (ContentPlaceHolder)Page.Master.FindControl("ContentPlaceHolder1"); //mpContentPlaceHolder.Controls.Add(widnow1); //var Encrypted_Password_File = EncryptionClass.EncryptRJ(filepass); //eBusiness.UploadExamFile(relativePath + did.ToString() + ".zip", Encrypted_Password_File, did, 2); //eBusiness.UploadAttachment(did, relativePath + did.ToString() + "Attached." + AttExt);//Address //cmnb.InsertIntoStudentLog(Session[sessionNames.userID_StudentOstad].ToString(), DateTime.Now.ToShortTimeString(), 8, 22, questionID.ToString()); //cmnb.InsertIntoStudentLog(Session[sessionNames.userID_StudentOstad].ToString(), DateTime.Now.ToShortTimeString(), 8, 21, questionID.ToString()); //eBusiness.UpdateQueizStatus(2, did, ""); //ScriptManager.RegisterStartupScript(this, GetType(), "btn_Save", "CloseModal();", true); } else { rwm.RadAlert("هیچ فایلی ارسال نشده است. لطفا مجددا فایل خود را ارسال نمایید", null, 100, "", ""); } } else { rwm.RadAlert("ابتدا فرم سوالات دانلود شود", null, 100, "خطا", ""); } } } else { rwm.RadAlert("فایل سوالات انتخاب شود ", null, 100, "خطا", ""); } } }
protected void btnSaveQ_Att_Click(object sender, EventArgs e) { if (Page.IsValid) { if (fileUploader.HasFile) { string QExt = "pdf"; string AttExt = "jpg"; string fullPathQuestionFile = ""; string fullPathAttachFile = ""; string fullPathZipFile = ""; //helper class to transfer data to db var Q_Att_Dto = new ExamQuestionDto(); ViewState["Qs_Attach22"] = null; if (AttachUploader.HasFile) { AttExt = CommonBusiness.getFileExtension(AttachUploader.FileName).ToLower(); } if ((QExt != "pdf" && QExt != "jpg") || (AttExt != "pdf" && AttExt != "jpg")) { rwm.RadAlert("فرمت صحیح انتخاب گردد", null, 100, "خطا", ""); } else { int qId = int.Parse(Request.QueryString["qID"].ToString()); string did = Request.QueryString["did"].ToString(); string saatExam = Request.QueryString["examTime"].ToString(); string dateExam = Request.QueryString["examDate"].ToString(); int? cityIdQ2 = int.Parse(Request.QueryString["cityId"].ToString()); int statusQ2 = int.Parse(Request.QueryString["statusQ2"].ToString()); int statusQ1 = int.Parse(Request.QueryString["statusQ1"].ToString()); int?cityIDQ2 = null; if (statusQ1 == 3 && statusQ2 != -1) { cityIDQ2 = cityIdQ2; } var examQ = Ebusiness.GetExamQuestionsbyDid(did, null, cityIDQ2); if (examQ != null && bool.Parse(examQ.Rows[0]["TemplateDownloaded"].ToString()) == true) { var userID = Session[sessionNames.userID_StudentOstad].ToString(); //var questionID = examQ.AsEnumerable().Select(s => s.Field<int>("ID")).FirstOrDefault(); var relativePath = $"~/QueizPapers/{examQ.Rows[0]["Term"].ToString()}/{userID}/pdfFiles/{did.ToString()}"; string absolutePath = Server.MapPath(relativePath); byte[] hashPassword = Convert.FromBase64String(examQ.Rows[0]["Password"].ToString()); string filepass = EncryptionClass.DecryptRJ256(hashPassword); //string filepass = generaterandomstr(8); //اگر امتحان لغو کلی شده برای همه شهرها if (qId > 0 && (statusQ2 == 8 || statusQ2 == 11) && cityIdQ2 == -1) { fullPathZipFile = $"{absolutePath}/{did.ToString()}_canceled_1.zip"; //Q_Att_Dto.HashedPass = EncryptionClass.EncryptRJ(filepass); } //اگر امتحان لغو شده برای برای یه شهر خاص else if (qId > 0 && (statusQ2 == 8 || statusQ2 == 11) && cityIdQ2 > 0) { fullPathZipFile = $"{absolutePath}/{did.ToString()}_canceled_2.zip"; //Q_Att_Dto.HashedPass = EncryptionClass.EncryptRJ(filepass); } else { fullPathZipFile = $"{absolutePath}/{did.ToString()}.zip"; } //Directory.GetDirectories(absolutePath).ToList().ForEach(dir => Directory.Delete(dir, true)); //Directory.GetFiles(absolutePath, "*.*").ToList().ForEach(file => File.Delete(file)); if (fileUploader.HasFile)//آپلود سوالات { Q_Att_Dto.IsExistQuestionFile = true; Q_Att_Dto.Did = did.ToString(); Q_Att_Dto.QuestionId = qId.ToString(); //questionID.ToString(); Q_Att_Dto.UserId = userID; Q_Att_Dto.HashedPass = examQ.Rows[0]["Password"].ToString(); //EncryptionClass.EncryptRJ(filepass); Q_Att_Dto.RelativePath = relativePath; QExt = CommonBusiness.getFileExtension(fileUploader.FileName).ToLower(); if (!Directory.Exists(absolutePath)) { Directory.CreateDirectory(absolutePath); } fullPathQuestionFile = $"{absolutePath}/{did}.{QExt}"; if (QExt == "pdf" || QExt == "jpg") { //var file = fileUploader.PostedFile; var qfSaved = SaveFile(fileUploader.PostedFile, absolutePath, did + "." + QExt); if (!qfSaved) { rwm.RadAlert("خطا در روند بارگذاری فایل سوالات! لطفا مجدداً بارگذاری نمائید.", null, 100, "", ""); return; } using (ZipFile zip = new ZipFile()) { zip.Password = filepass; FileStream stream = null; using (stream = new FileStream(fullPathQuestionFile, FileMode.Open, FileAccess.ReadWrite)) { zip.AddEntry(did.ToString() + "." + QExt, stream); zip.Save(fullPathZipFile); } } } } if (AttachUploader.HasFile)//آپلود سوالت پیوست { Q_Att_Dto.IsExistAttachFile = true; AttExt = CommonBusiness.getFileExtension(AttachUploader.FileName).ToLower(); fullPathAttachFile = $"{absolutePath}/{did}Attached.{AttExt}"; if (AttExt == "jpg" || AttExt == "pdf") { Q_Att_Dto.AttExt = AttExt; var att_fSaved = SaveAttachFile(AttachUploader.PostedFile, absolutePath, did + "Attached." + AttExt); if (!att_fSaved) { rwm.RadAlert("خطا در روند بارگذاری فایل پیوست سوالات! لطفا مجدداً بارگذاری نمائید.", null, 100, "", ""); return; } using (ZipFile zip = new ZipFile(fullPathZipFile)) { zip.Password = filepass; FileStream stream = null; stream = new FileStream(fullPathAttachFile, FileMode.Open, FileAccess.ReadWrite); zip.AddEntry(did.ToString() + "Attached." + AttExt, stream); zip.Save(fullPathZipFile); } } } Directory.GetFiles(absolutePath, "*.*").Where(w => !w.Contains(".zip")).ToList().ForEach(file => File.Delete(file)); if (File.Exists(fullPathZipFile)) { var whiteTape = Server.MapPath("~/University/Theme/images/whitePaper.jpg"); var questioHeaderTemplate = Server.MapPath("~/University/Theme/images/QuestionHeaderTemplate.jpg"); var examQ_Detail = Ebusiness.Get_ExamdetailbyDid(did, null, cityIDQ2);//.AsEnumerable().Select(s=>s).FirstOrDefault(); var constQuestionFileInfo = new ExamStudentDTO(); constQuestionFileInfo.TypeNimsal = examQ_Detail.Rows[0]["typeNimsal"].ToString(); constQuestionFileInfo.CourseTitle = examQ_Detail.Rows[0]["namedars"].ToString(); constQuestionFileInfo.ProfossorFullName = examQ_Detail.Rows[0]["osname"].ToString(); constQuestionFileInfo.ExamDate = examQ_Detail.Rows[0]["dateexam"].ToString(); constQuestionFileInfo.ExamTime = examQ_Detail.Rows[0]["saatexam"].ToString(); constQuestionFileInfo.KeyCode = examQ_Detail.Rows[0]["keyCode"].ToString(); constQuestionFileInfo.ExamDuration = examQ_Detail.Rows[0]["examTime"].ToString(); constQuestionFileInfo.Calculator = examQ_Detail.Rows[0]["calculator"].ToString(); constQuestionFileInfo.Note = examQ_Detail.Rows[0]["note"].ToString(); constQuestionFileInfo.LowBook = examQ_Detail.Rows[0]["LowBook"].ToString(); constQuestionFileInfo.ClassCode = examQ_Detail.Rows[0]["ClassCode"].ToString(); constQuestionFileInfo.Grade = examQ_Detail.Rows[0]["magh"].ToString(); constQuestionFileInfo.Major = examQ_Detail.Rows[0]["nameresh"].ToString(); Ebusiness.ChangeTemplateOfQuestion(absolutePath, did, filepass, constQuestionFileInfo, questioHeaderTemplate, whiteTape, userID, cityIDQ2); var lastPdfPath = $"{Request.Url.GetLeftPart(UriPartial.Authority)}/{relativePath.Replace("~/", "")}/{userID}/{did}_Momtahen_{userID}_1.pdf?ts={DateTime.Now.Ticks}"; PdfFileSource = lastPdfPath; ViewState["Qs_Attach22"] = Q_Att_Dto; SectionManagement(false, false, true); } else { rwm.RadAlert("هیچ فایلی ارسال نشده است. لطفا مجددا فایل خود را ارسال نمایید", null, 100, "", ""); } } else { rwm.RadAlert("ابتدا فرم سوالات دانلود شود", null, 100, "خطا", ""); } } } else { rwm.RadAlert("فایل سوالات انتخاب شود ", null, 100, "خطا", ""); } } }