protected void grd_CourseList_ItemCommand(object sender, GridCommandEventArgs e) { string ipaddress; ipaddress = Request.ServerVariables["HTTP_X_FORWARDED_FOR"]; if (ipaddress == "" || ipaddress == null) { ipaddress = Request.ServerVariables["REMOTE_ADDR"]; } var ip = (HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"] != null && HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"] != "") ? HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"] : HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"]; if (ip.Contains(",")) { ip = ip.Split(',').First(); } var cmdArgs = e.CommandArgument.ToString().Split(','); var did = cmdArgs[0]; var cityIdQ2 = int.Parse(cmdArgs[1]); var statusQ1 = int.Parse(cmdArgs[2]); var statusQ2 = int.Parse(cmdArgs[3]); int?cityIDQ2 = null; if (statusQ1 == 3 && statusQ2 != -1) { cityIDQ2 = cityIdQ2; } var mainFormatClick = false; var examQ_Detail = EBusiness.Get_ExamdetailbyDid(did, null, cityIDQ2); if (e.CommandName == "DlQuestionMainFormat")//فرمت اصلی سوالات بدون تغیرات { var relativePath = "QueizPapers/" + examQ_Detail.Rows[0]["tterm"].ToString() + "/" + examQ_Detail.Rows[0]["code_ostad"].ToString() + "/pdffiles/" + did.ToString(); // string path = Server.MapPath($"~/{relativePath}"); CB.InsertIntoUserLogwithIP(int.Parse(Session[sessionNames.userID_Karbar].ToString()), DateTime.Now.ToShortTimeString(), int.Parse(Session[sessionNames.appID_Karbar].ToString()), 67, ip, " دانلود سوالات امتحان مشخصه " + did.ToString()); DownloadZipFile(relativePath, $"{did.ToString()}.zip"); mainFormatClick = true; //SendFileToUser(path + "\\" + e.CommandArgument.ToString() + ".zip"); } if (e.CommandName == "DlQuestionSinglePage" && !mainFormatClick) //حاصل از فایل سوال وپیوست بدون اطلاعات دانشجو pdf فایل { var path = $"QueizPapers/{examQ_Detail.Rows[0]["tterm"].ToString()}/{examQ_Detail.Rows[0]["code_ostad"].ToString()}/pdffiles/{did.ToString()}"; CB.InsertIntoUserLogwithIP(int.Parse(Session[sessionNames.userID_Karbar].ToString()), DateTime.Now.ToShortTimeString(), int.Parse(Session[sessionNames.appID_Karbar].ToString()), 67, ip, " دانلود سوالات امتحان مشخصه " + did.ToString()); 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(); TextBox lbl_Password = (TextBox)e.Item.FindControl("lbl_Password"); var pass = lbl_Password.Text; var whiteTape = Server.MapPath("~/University/Theme/images/whitePaper.jpg"); var questioHeaderTemplate = Server.MapPath("~/University/Theme/images/QuestionHeaderTemplate.jpg"); var userID = Session[sessionNames.userID_Karbar].ToString(); var absPath = Server.MapPath($"/{path}"); var res = EBusiness.ChangeTemplateOfQuestion(absPath, did.ToString(), pass, constQuestionFileInfo, questioHeaderTemplate, whiteTape, userID, cityIDQ2); DownloadZipFile($"{path}/{userID}", $"{did}_Momtahen_{userID}_1.zip"); } if (e.CommandName == "DlQuestionMergedFiles" && !mainFormatClick)//حاصل از فایل سوال وپیوست به همراه اطلاعات دانشجو pdf فایل { var path = $"QueizPapers/{examQ_Detail.Rows[0]["tterm"].ToString()}/{examQ_Detail.Rows[0]["code_ostad"].ToString()}/pdffiles/{did.ToString()}"; 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(); var dt = EBusiness.ExamAnswerSheetbyDid(did, int.Parse(Session[sessionNames.userID_Karbar].ToString())); var studentList = dt.AsEnumerable().Select(x => new ExamStudentDTO() { FirstName = x.Field <string>("stFirstName") ?? "", LastName = x.Field <string>("stLastName") ?? "", StudentCode = x.Field <string>("stcode") ?? "", Grade = x.Field <string>("magh") ?? "", Major = x.Field <string>("nameresh") ?? "", SeatHeader = x.Field <string>("SeatHeader") ?? "", SeatNumber = x.Field <int?>("SeatNumber") }).ToList(); TextBox lbl_Password = (TextBox)e.Item.FindControl("lbl_Password"); var pass = lbl_Password.Text; var whiteTape = Server.MapPath("~/University/Theme/images/whitePaper.jpg"); var questioHeaderTemplate = Server.MapPath("~/University/Theme/images/QuestionHeaderTemplate.jpg"); var userID = Session[sessionNames.userID_Karbar].ToString(); var absPath = Server.MapPath($"/{path}"); var res = EBusiness.GeneratePdfQuestionForStudents(absPath, did.ToString(), pass, questioHeaderTemplate, whiteTape, userID, constQuestionFileInfo, studentList, cityIDQ2); DownloadZipFile($"{path}/{userID}", $"{did}_Momtahen_{userID}_2.zip"); } }
protected void grd_Class_ItemCommand(object sender, Telerik.Web.UI.GridCommandEventArgs e) { if (e.CommandName == "Filter") { return; } string did = "0"; var dtdet = new DataTable(); var path = string.Empty; var absPath = string.Empty; var pass = string.Empty; var hdnPassword = (HiddenField)e.Item.FindControl("hdnPassword"); if (e.CommandName == "ShowNewHeaderForAll" || e.CommandName == "ShowNewHeader" || e.CommandName == "ShowOldHeader") { did = e.CommandArgument.ToString(); dtdet = examBusiness.Get_ExamdetailbyDid(did); path = $"QueizPapers/{dtdet.Rows[0]["tterm"].ToString()}/{dtdet.Rows[0]["code_ostad"].ToString()}/pdffiles/{e.CommandArgument.ToString()}"; absPath = Server.MapPath($"~/{path}"); var base64HashesPassword = Convert.FromBase64String(hdnPassword.Value); pass = EncryptionClass.DecryptRJ256(base64HashesPassword); } var QuestionId = Convert.ToInt32(e.CommandArgument.ToString()); var constQuestionFileInfo = new ExamStudentDTO(); var whiteTape = Server.MapPath("~/University/Theme/images/whitePaper.jpg"); var questioHeaderTemplate = Server.MapPath("~/University/Theme/images/QuestionHeaderTemplate.jpg"); var userID = Session[sessionNames.userID_Karbar].ToString(); switch (e.CommandName) { case "ShowNewHeaderForAll": constQuestionFileInfo = dtdet.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() }).FirstOrDefault(); var dt = examBusiness.ExamAnswerSheetbyDid(did, int.Parse(Session[sessionNames.userID_Karbar].ToString())); var studentList = dt.AsEnumerable().Select(x => new ExamStudentDTO() { FirstName = x.Field <string>("stFirstName"), LastName = x.Field <string>("stLastName"), StudentCode = x.Field <string>("stcode"), Grade = x.Field <string>("magh"), Major = x.Field <string>("nameresh"), SeatHeader = x.Field <string>("SeatHeader"), SeatNumber = x.Field <int?>("SeatNumber") }).ToList(); examBusiness.GeneratePdfQuestionForStudents(absPath, did.ToString(), pass, questioHeaderTemplate, whiteTape, userID, constQuestionFileInfo, studentList); Session["BigFile"] = true; ShowFiles($"{path}/{userID}/{did}_Momtahen_{userID}_2.zip", pass); break; case "ShowNewHeader": constQuestionFileInfo = dtdet.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() }).FirstOrDefault(); examBusiness.ChangeTemplateOfQuestion(absPath, e.CommandArgument.ToString(), pass, constQuestionFileInfo, questioHeaderTemplate, whiteTape, userID); ShowFiles($"{path}/{userID}/{did}_Momtahen_{userID}_1.zip", pass); break; case "ShowOldHeader": constQuestionFileInfo = dtdet.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() }).FirstOrDefault(); examBusiness.ChangeTemplateOfQuestion(absPath, e.CommandArgument.ToString(), pass, constQuestionFileInfo, questioHeaderTemplate, whiteTape, userID); ShowFiles($"{path}/{did}.zip", pass); break; case "ApproveNewHeader": examBusiness.SetApproveNewHeader(QuestionId, true); BindGrid = true; grd_Class.Rebind(); break; case "RejectNewHeader": examBusiness.SetApproveNewHeader(QuestionId, false); BindGrid = true; grd_Class.Rebind(); break; } }
//############################################# 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, "خطا", ""); } } }
void DownloadGeneratedPdf(DataRow row) { string did = "-1"; string QuesId = "-1"; int? cityId = null; int q2Status = -1; int q1Status = -1; string fullPathZipFile; //=================================== did = row["coursecode"].ToString(); QuesId = row["QuestionId"].ToString(); cityId = int.Parse(row["cityId"].ToString()); q2Status = int.Parse(row["q2Status"].ToString()); q1Status = int.Parse(row["Status"].ToString()); int?cityIDQ2 = null; if (q2Status != -1 && q1Status == 3) { cityIDQ2 = cityId; } var quizPapere = EBusiness.ShowQueizPaperByDid(did, cityIDQ2); var dynamicPath = "~/QueizPapers/" + quizPapere.Rows[0]["tterm"].ToString() + "/" + quizPapere.Rows[0]["code_ostad"].ToString() + "/pdffiles/" + quizPapere.Rows[0]["coursecode"].ToString(); string path = Server.MapPath(dynamicPath); if (cityIDQ2 == -1) { fullPathZipFile = $"{path}/{did.ToString()}_canceled_1.zip"; } else if (cityIDQ2 > 0) { fullPathZipFile = $"{path}/{did.ToString()}_canceled_2.zip"; } else { fullPathZipFile = $"{path}/{did.ToString()}.zip"; } byte[] base64HashesPassword = Convert.FromBase64String(quizPapere.Rows[0]["Password"].ToString()); string pass = EncryptionClass.DecryptRJ256(base64HashesPassword); var item = EBusiness.GetExamQuestionsbyDid(did, null, cityIDQ2); 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); 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(); var userID = Session[sessionNames.userID_Karbar].ToString(); EBusiness.ChangeTemplateOfQuestion(path, did.ToString(), pass, constQuestionFileInfo, questioHeaderTemplate, whiteTape, userID, cityIDQ2); var generatedPdfPath = (Server.MapPath($"{dynamicPath}/{userID}/{did}_Momtahen_{userID}_1.pdf")); fileByteArray = System.IO.File.ReadAllBytes(generatedPdfPath); var shortNameFile = $"{did}.pdf"; Response.Clear(); Response.ContentType = "application/force-download"; Response.AddHeader("content-disposition", "attachment;filename=" + shortNameFile); Response.BinaryWrite(fileByteArray); Response.End(); }
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, "خطا", ""); } } }