/// <summary> /// 上传前整理treeview中节点内容,并生成最终上传内容 /// </summary> /// <param name="treenode"></param> /// <param name="subjectkind"></param> /// <param name="subjectItem"></param> private void createSubjectItem(TreeNode treenode, string subjectkind, ref SubjectItem subjectItem) { //SubjectItem subjectItem = new SubjectItem(); for (int i = 0; i < treenode.Nodes.Count; i++) { SubjectItem subjectItemtemp = new SubjectItem();//试题属性与内容 subjectItemtemp.subjectkinds = subjectkind; Subject subject = (Subject)treenode.Nodes[i].Tag; string html = subject.subjecthtmlcontent; //取html中body内容 if (html != null && !html.Equals("")) try { html = HtmlUtil.HtmlFragmentClear(html); } catch { } subjectItemtemp.subjecthtmlcontent = html; //subjectItemtemp.subjecthtmlcontent = HtmlUtil.htmlreplace(subjectItemtemp.subjecthtmlcontent); //匹配图片中的img src内容返回src地址 string timedir = "/files/tsubjectfile/" + DateTime.Now.Year + "/" + DateTime.Now.Month + "/" + DateTime.Now.Day + "/" + "image/"; //string[] img = HtmlUtil.imgSrcForName(ref subjectItemtemp.subjecthtmlcontent, timedir); string[] img = null; subjectItemtemp.subjecttextcontent = subject.subjecttextcontent; subjectItemtemp.sequence = subject.套卷序号; subjectItemtemp.selectItemCount = subject.选项个数; subjectItemtemp.subjecttype = subject.题目类型.GetHashCode().ToString(); //subjectItemtemp.base64str = Convert.ToBase64String(subject.subjectbinarycontent); //过滤html中vml内容 //subjectItemtemp.subjectfilterhtmlcontent = HtmlUtil.htmlreplace(subjectItemtemp.subjecthtmlcontent); if (subject.answer != null && subject.answer.subjecthtmlanswer!=null)//处理答案相关数据 { //subjectItemtemp.answerbinarycontent = subject.answer.subjectbinaryanswer; string htmla = subject.answer.subjecthtmlanswer; //取html中body内容 try { htmla = HtmlUtil.HtmlFragmentClear(htmla); } catch { } subjectItemtemp.answerhtmlcontent = htmla; //string[] answerimg = HtmlUtil.imgSrcForName(ref subjectItemtemp.answerhtmlcontent, timedir); string[] answerimg = null; subjectItemtemp.img = new string[img.Length + answerimg.Length]; img.CopyTo(subjectItemtemp.img, 0); answerimg.CopyTo(subjectItemtemp.img, img.Length); string[] answerimgdata = HtmlUtil.imgDataSrcForName(ref subjectItemtemp.answerhtmlcontent,timedir); subjectItemtemp.answertextcontent = subject.answer.subjecttextanswer; //过滤html中vml内容,单选题、多选题、对错题才用存文本形式 if (subjectItemtemp.subjecttype.Equals(Subject.SubjecttypeEnum.单选题) || subjectItemtemp.subjecttype.Equals(Subject.SubjecttypeEnum.多选题) || subjectItemtemp.subjecttype.Equals(Subject.SubjecttypeEnum.判断题)) { //subjectItemtemp.answerfilterhtmlcontent = subject.answer.subjecttextanswer; subjectItemtemp.answerhtmlcontent = subject.answer.subjecttextanswer; } } else { subjectItemtemp.img = img; } subjectItem.subSubjectItemList.Add(subjectItemtemp); if (treenode.Nodes[i].Nodes.Count > 0) { createSubjectItem(treenode.Nodes[i], subjectkind, ref subjectItemtemp); } } }
/// <summary> /// 压缩并上传拆分后的题目 /// </summary> /// <param name="onlyZip"></param> private void zipFileAndUpload(bool onlyZip) { try { if (this.mainContent1.WordBrowers.document == null) { MessageBox.Show("请先在程序中打开word文件。"); return; } if ( testpaper.学科 == null || testpaper.学科.Equals("")) { MessageBox.Show("请先填写完整试卷信息。"); return; } //if (!Regex.IsMatch(testpaper.年份, @"^\d{4}$")) //{ // MessageBox.Show("年份只能为4位数字。"); // return; //} if (!onlyZip && MessageBox.Show("确定开始上传试卷吗?", "提示", MessageBoxButtons.OKCancel) != DialogResult.OK) { return; } Paper root = new Paper(); for (int i = 0; i < this.treeViewSubject.Nodes.Count; i++) { SubjectItem subjectItem = new SubjectItem(); createSubjectItem(this.treeViewSubject.Nodes[i], this.treeViewSubject.Nodes[i].Text, ref subjectItem); root.bigSubject.Add(subjectItem); } if (root.bigSubject.Count > 0) { if (root.bigSubject[0].subSubjectItemList.Count == 0) { MessageBox.Show("文件未被识别无法打包上传。"); return; } } //root.source = testpaper.来源; //root.stage = testpaper.阶段; //XmlDocument xmldocstage = new XmlDocument(); //xmldocstage.Load(Application.StartupPath + MartianSimple.stagefilename); //XmlNodeList xnlstage = xmldocstage.SelectNodes("//Result"); //for (int i = 0; i < xnlstage.Count; i++) //{ // XmlElement xmlstage = (XmlElement)xnlstage[i]; // string str = xmlstage.GetAttribute("Id"); // if (testpaper.阶段.Equals(str)) // { // root.stage = (i + 1).ToString(); // break; // } //} //root.xueke = testpaper.学科; XmlDocument xmldocclass = new XmlDocument(); xmldocclass.Load(Application.StartupPath + MartianSimple.xuekefilename); XmlNodeList xnl = xmldocclass.SelectNodes("//Id"); XmlNodeList xnltitle = xmldocclass.SelectNodes("//Title"); for (int i = 0; i < xnl.Count; i++) { //XmlNode xmlrr = xnl[i]; //XmlNode stagexmode = xmlrr.SelectSingleNode("//Title"); string strp = xnltitle[i].InnerText; string strb = testpaper.学科; if (strp.Equals(strb)) { //XmlNode stagexmodeId = xnl[i].SelectSingleNode("//Id"); root.xueke = xnl[i].InnerText; break; } } //root.year = testpaper.年份; root.tag = testpaper.标签; root.papername = testpaper.试卷名称; XmlSerializer xmls = new XmlSerializer(typeof(Paper)); StringWriter sw = new StringWriter(); xmls.Serialize(sw, root); XmlDocument xmldoc = new XmlDocument(); xmldoc.LoadXml(sw.ToString()); string xmlpath = System.Windows.Forms.Application.StartupPath + HtmlUtil.uploadfile + "uploadfile.xml"; if (!Directory.Exists(System.Windows.Forms.Application.StartupPath + HtmlUtil.uploadfile)) { Directory.CreateDirectory(System.Windows.Forms.Application.StartupPath + HtmlUtil.uploadfile); } xmldoc.Save(xmlpath); //压缩上传内容 ZipHelper.CreateZip(System.Windows.Forms.Application.StartupPath + HtmlUtil.uploadfile, System.Windows.Forms.Application.StartupPath + @"\uploadfile.zip"); //Directory.Delete(System.Windows.Forms.Application.StartupPath + HtmlUtil.uploadfile, true); //上传文件内容 if (!onlyZip) { XmlDocument xmldocc = new XmlDocument(); xmldoc.Load(Application.StartupPath + "/LoginConfig.xml"); XmlNode address = xmldoc.SelectSingleNode("//address"); XmlNode account = xmldoc.SelectSingleNode("//account"); XmlNode password = xmldoc.SelectSingleNode("//password"); //使用webservice上传 //Model_WebServicePortClient client = new Model_WebServicePortClient("Model_WebServicePort", address.InnerText); //byte[] buffer = File.ReadAllBytes(System.Windows.Forms.Application.StartupPath + @"\uploadfile.zip"); ////StreamReader sr=new StreamReader(System.Windows.Forms.Application.StartupPath + @"\uploadfile.zip"); ////sr.Read(buffer,0,sr); //string base64String = System.Convert.ToBase64String(buffer, 0, buffer.Length); //client.itemupload("uploadfile.zip", base64String); //client.ClientCredentials.UserName.UserName = account.InnerText; //client.ClientCredentials.UserName.Password = password.InnerText; //使用resetful上传 HttpHelper httphelper = new HttpHelper(); NameValueCollection namevaluec = new NameValueCollection(); namevaluec.Add("token", "apiupload"); //namevaluec.Add("xueke", this.testpaper.学科); namevaluec.Add("username", account.InnerText); string loginrequest = httphelper.postFile("http://" + address.InnerText + "/api/Item/post", System.Windows.Forms.Application.StartupPath + @"\uploadfile.zip", namevaluec); //string loginrequest = httphelper.Upload_Request("http://*****:*****@"\uploadfile.zip", System.Windows.Forms.Application.StartupPath + @"\uploadfile.zip", namevaluec); //string loginrequest = httphelper.UploadFileEx("http://" + address.InnerText + "/api/Item/post", System.Windows.Forms.Application.StartupPath + @"\uploadfile.zip", null, null, namevaluec, null);//http://" + address.InnerText + "/api/Item/post XmlDocument docrequest = new XmlDocument(); docrequest.LoadXml(loginrequest.Trim()); XmlNode statusnode = docrequest.SelectSingleNode("//Status"); if (statusnode.InnerText.Equals("fail")) { XmlNode errormessagenode = docrequest.SelectSingleNode("//ErrorMessage"); MessageBox.Show(errormessagenode.InnerText); } else { MessageBox.Show("上传成功"); //if (Directory.Exists(System.Windows.Forms.Application.StartupPath + HtmlUtil.uploadfile)) // Directory.Delete(System.Windows.Forms.Application.StartupPath + HtmlUtil.uploadfile, true); } } // File.Delete(System.Windows.Forms.Application.StartupPath + @"\uploadfile.zip"); } catch (DirectoryNotFoundException ex) { MessageBox.Show("上传失败," + ex.Message); } catch (XmlException xmle) { MessageBox.Show("上传失败,请检查服务器地址设置是否正确。"); } catch (Exception ex) { MessageBox.Show("上传失败,"+ex.Message); } }
/// <summary> /// 上传前整理treeview中节点内容,并生成最终上传内容 /// </summary> /// <param name="treenode"></param> /// <param name="subjectkind"></param> /// <param name="subjectItem"></param> private void createSubjectItem(TreeNode treenode, string subjectkind, ref SubjectItem subjectItem) { Subject subjectTreeRoot = (Subject)treenode.Tag; for (int i = 0; i < treenode.Nodes.Count; i++) { SubjectItem subjectItemtemp = new SubjectItem(); //插入节信息 subjectItemtemp.subjectkinds = subjectkind; Subject subject = (Subject)treenode.Nodes[i].Tag; string html = subject.subjecthtmlcontent; html = Regex.Replace(html, @"^<p[^>]*>([\s\S]+?)</p>(.*)", "$1$2", RegexOptions.IgnoreCase); //循环替换图片中的\r\n while (true) { string tempHtml = html; html = Regex.Replace(html, @"<img(.+?)\r\n(.+?)>", "<img$1 $2>", RegexOptions.IgnoreCase | RegexOptions.Multiline | RegexOptions.Singleline); if (tempHtml == html) { break; } } html = Regex.Replace(html, @"^<pre[^>]*>([\s\S]+?)</pre>(.*)", "$1$2", RegexOptions.IgnoreCase); subjectItemtemp.subjecthtmlcontent = html; //替换答案中图片中的\r\n html = subject.answer.subjecthtmlanswer; while (true) { string tempHtml = html; html = Regex.Replace(html, @"<img(.+?)\r\n(.+?)>", "<img$1 $2>", RegexOptions.IgnoreCase | RegexOptions.Multiline | RegexOptions.Singleline); if (tempHtml == html) { break; } } subject.answer.subjecthtmlanswer = html; //匹配图片中的img src内容返回src地址 //string timedir = "/files/tsubjectfile/" + DateTime.Now.Year + "/" + DateTime.Now.Month + "/" + DateTime.Now.Day + "/" + "image/"; string[] img = HtmlUtil.imgSrcForName(ref subjectItemtemp.subjecthtmlcontent, uploadimgsdic);//防火html中img图片地址列表,并修改地址为网络地址 subjectItemtemp.subjecttextcontent = subject.subjecttextcontent; subjectItemtemp.score = subject.分值; subjectItemtemp.sequence = subject.套卷序号; subjectItemtemp.selectItemCount = subject.选项个数; subjectItemtemp.isobjective = subject.客观题.GetHashCode(); subjectItemtemp.subjecttype = subject.题目类型.GetHashCode().ToString(); //过滤html中vml内容,存入过滤后的字段 //subjectItemtemp.subjectfilterhtmlcontent = SubjectTypeMark(subjectItemtemp.subjecthtmlcontent, subject.题目类型.GetHashCode()); if (subject.answer != null && subject.answer.subjecthtmlanswer != null)//处理答案相关数据 { //subjectItemtemp.answerbinarycontent = subject.answer.subjectbinaryanswer; string htmla = subject.answer.subjecthtmlanswer; subjectItemtemp.answerhtmlcontent = htmla; string[] answerimg = HtmlUtil.imgSrcForName(ref subjectItemtemp.answerhtmlcontent, uploadimgsdic); subjectItemtemp.img = new string[img.Length + answerimg.Length]; img.CopyTo(subjectItemtemp.img, 0); answerimg.CopyTo(subjectItemtemp.img, img.Length); subjectItemtemp.answertextcontent = subject.answer.subjecttextanswer; //过滤html中vml内容,单选题、多选题、对错题才用存文本形式 if (subjectItemtemp.subjecttype.Equals(Subject.SubjecttypeEnum.单选题) || subjectItemtemp.subjecttype.Equals(Subject.SubjecttypeEnum.多选题) || subjectItemtemp.subjecttype.Equals(Subject.SubjecttypeEnum.判断题)) { subjectItemtemp.answerhtmlcontent = subject.answer.subjecttextanswer.Trim(); } } else { subjectItemtemp.img = img; } SubjectTypeMarkAndCreateOption(ref subjectItemtemp, subject.题目类型.GetHashCode());//选项标识并列出选项 subjectItem.subSubjectItemList.Add(subjectItemtemp); if (treenode.Nodes[i].Nodes.Count > 0) { createSubjectItem(treenode.Nodes[i], subjectkind, ref subjectItemtemp); } } }
/// <summary> /// 压缩并上传拆分后的题目 /// </summary> /// <param name="onlyZip"></param> private void zipFileAndUpload(bool onlyZip) { try { Paper paper = new Paper(); for (int i = 0; i < this.treeViewSubject.Nodes.Count; i++) { SubjectItem subjectItem = new SubjectItem(); //遍历子节点中的内容 createSubjectItem(this.treeViewSubject.Nodes[i], this.treeViewSubject.Nodes[i].Text, ref subjectItem); subjectItem.subjecttextcontent = this.treeViewSubject.Nodes[i].Text; paper.bigSubject.Add(subjectItem); } if (paper.bigSubject.Count > 0) { if (paper.bigSubject[0].subSubjectItemList.Count == 0) { MessageBox.Show("文件未被识别或第一个大题没有试题。", "提示"); return; } } XmlDocument xmldocclass = new XmlDocument(); xmldocclass.Load(Application.StartupPath + xuekefilename); XmlNodeList xnl = xmldocclass.SelectNodes("//Id"); XmlNodeList xnltitle = xmldocclass.SelectNodes("//Title"); for (int i = 0; i < xnl.Count; i++) { //XmlNode xmlrr = xnl[i]; //XmlNode stagexmode = xmlrr.SelectSingleNode("//Title"); string strp = xnltitle[i].InnerText; string strb = testpaper.学科; if (strp.Equals(strb)) { //XmlNode stagexmodeId = xnl[i].SelectSingleNode("//Id"); paper.xueke = xnl[i].InnerText; break; } } // paper.year = testpaper.年份; paper.tag = testpaper.标签; paper.papername = testpaper.试卷名称; string xmlpaper = PaperSerializerXml(paper);//生成xml文件 //string jsonpaper= PaperSerializerJson(paper);//序列化成json数据 //MessageBox.Show(jsonpaper); //压缩上传内容 if (onlyZip) { if (this.saveFileDialog2.ShowDialog() == DialogResult.OK) { string fileName = saveFileDialog2.FileName; try { ZipHelper.CreateZip(System.Windows.Forms.Application.StartupPath + HtmlUtil.uploadfile, fileName); } catch { } } } //else //{ // ZipHelper.CreateZip(System.Windows.Forms.Application.StartupPath + HtmlUtil.uploadfile, System.Windows.Forms.Application.StartupPath + @"\uploadfile.zip"); //} //Directory.Delete(System.Windows.Forms.Application.StartupPath + HtmlUtil.uploadfile, true); //上传文件内容 if (!onlyZip) { //UploadPaperZip();//上床压缩包 PostPaper(xmlpaper); } // File.Delete(System.Windows.Forms.Application.StartupPath + @"\uploadfile.zip"); } catch (DirectoryNotFoundException ex) { MessageBox.Show("上传失败," + ex.Message, "错误"); } catch (XmlException xmle) { MessageBox.Show("上传失败,请检查服务器地址设置是否正确。", "错误"); } catch (Exception ex) { MessageBox.Show("上传失败," + ex.Message, "错误"); } }
/// <summary> /// 试题类型处理 /// 1.单(多)项选择题拆分选项。 /// 2.填空题添加标识 /// 3.判断题转换数据 /// </summary> /// <param name="subjectitem">题目内容</param> /// <param name="subjecttype">题目类型</param> /// <returns></returns> private void SubjectTypeMarkAndCreateOption(ref SubjectItem subjectitem, int subjecttype) { //html = HtmlUtil.htmlreplace(html); if (subjecttype == Subject.SubjecttypeEnum.单选题.GetHashCode() || subjecttype == Subject.SubjecttypeEnum.多选题.GetHashCode()) { subjectitem.subjecthtmlcontent = HtmlUtil.selectSubjectOptionMarkUpload(subjectitem.subjecthtmlcontent); string tempsubjecthtmlcontent = subjectitem.subjecthtmlcontent;//临时存放试题提干变量 int i = 0, startindex = 0, endindex = 0; bool isanswer = false; while ((endindex = tempsubjecthtmlcontent.IndexOf(MatchArray.Select_Option, startindex)) > 0) { if (i == 0) { var subjecthtmlcontent = TagsChecker.fix(subjectitem.subjecthtmlcontent.Substring(0, endindex)); subjecthtmlcontent = Regex.Replace(subjecthtmlcontent, @"<p[\s]*>(( )|[\s])*</p>", "", RegexOptions.IgnoreCase); subjecthtmlcontent = Regex.Replace(subjecthtmlcontent, @"<pre[\s]*>(( )|[\s])*</pre>", "", RegexOptions.IgnoreCase); //answercontent = Regex.Replace(answercontent, @"\\r\\n", "", RegexOptions.IgnoreCase); subjecthtmlcontent = Regex.Replace(subjecthtmlcontent, @"^<p[^>]*>([\s\S]+?)</p>(.*)", "$1$2", RegexOptions.IgnoreCase); subjecthtmlcontent = Regex.Replace(subjecthtmlcontent, @"^<pre[^>]*>([\s\S]+?)</pre>(.*)", "$1$2", RegexOptions.IgnoreCase); subjectitem.subjecthtmlcontent = subjecthtmlcontent; i++; startindex = endindex + MatchArray.Select_Option.Length; continue; } else { isanswer = false; //判断答案是否正确 isanswer = SelectAnswerPair(subjectitem, i, isanswer); string answercontent = TagsChecker.fix(tempsubjecthtmlcontent.Substring(startindex, endindex - startindex).Trim()); answercontent = Regex.Replace(answercontent, @"<p[\s]*>(( )|[\s])*</p>", "", RegexOptions.IgnoreCase); answercontent = Regex.Replace(answercontent, @"<pre[\s]*>(( )|[\s])*</pre>", "", RegexOptions.IgnoreCase); //answercontent = Regex.Replace(answercontent, @"\\r\\n", "", RegexOptions.IgnoreCase); answercontent = Regex.Replace(answercontent, @"^<p[^>]*>([\s\S]+?)</p>(.*)", "$1$2", RegexOptions.IgnoreCase); answercontent = Regex.Replace(answercontent, @"^<pre[^>]*>([\s\S]+?)</pre>(.*)", "$1$2", RegexOptions.IgnoreCase); Answer answer = new Answer(answercontent, isanswer, i); subjectitem.answers.Add(answer); } i++; startindex = endindex + MatchArray.Select_Option.Length; } if (i > 0) { isanswer = false; isanswer = SelectAnswerPair(subjectitem, i, isanswer); string answercontent = TagsChecker.fix(tempsubjecthtmlcontent.Substring(startindex).Trim()); answercontent = Regex.Replace(answercontent, @"<p[\s]*>(( )|[\s])*</p>", "", RegexOptions.IgnoreCase); answercontent = Regex.Replace(answercontent, @"<pre[\s]*>(( )|[\s])*</pre>", "", RegexOptions.IgnoreCase); //answercontent = Regex.Replace(answercontent, @"\\r\\n", "", RegexOptions.IgnoreCase); answercontent = Regex.Replace(answercontent, @"^<p[^>]*>([\s\S]+?)</p>(.*)", "$1$2", RegexOptions.IgnoreCase); answercontent = Regex.Replace(answercontent, @"^<pre[^>]*>([\s\S]+?)</pre>(.*)", "$1$2", RegexOptions.IgnoreCase); Answer answer = new Answer(answercontent, isanswer, i); subjectitem.answers.Add(answer); } return; } if (subjecttype == Subject.SubjecttypeEnum.填空题.GetHashCode()) { subjectitem.subjecthtmlcontent = HtmlUtil.spacePlaceMarkUpLoad(subjectitem.subjecthtmlcontent); Regex regspacenum = new Regex(MatchArray.Space_Num); int i = 0; // 取得匹配项列表 while (regspacenum.IsMatch(subjectitem.subjecthtmlcontent)) { subjectitem.subjecthtmlcontent = regspacenum.Replace(subjectitem.subjecthtmlcontent, string.Format(MatchArray.Space_Num_Replace, i+1), 1); i++; } int sort = 0;//答案顺序 string[] answerarr = subjectitem.answertextcontent.Split('|'); foreach (string content in answerarr) { sort++; Answer answer = new Answer(content, true, sort); subjectitem.answers.Add(answer); } while (sort < i) { sort++; Answer answer = new Answer("", true, sort); subjectitem.answers.Add(answer); } return; } if (subjecttype == Subject.SubjecttypeEnum.判断题.GetHashCode()) { string answerstr = "0"; if (subjectitem.answertextcontent.IndexOf("正确") >= 0 || subjectitem.answertextcontent.IndexOf("对") >= 0) answerstr = "1"; Answer answer = new Answer(answerstr, true, 1); subjectitem.answers.Add(answer); return; } Answer answerother = new Answer(subjectitem.answerhtmlcontent, true, 1); subjectitem.answers.Add(answerother); }
private static bool SelectAnswerPair(SubjectItem subjectitem, int i, bool isanswer) { if (subjectitem.answertextcontent != null) { switch (i) { case 1: if (subjectitem.answertextcontent.Contains("A") || subjectitem.answertextcontent.Contains("a")) isanswer = true; break; case 2: if (subjectitem.answertextcontent.Contains("B") || subjectitem.answertextcontent.Contains("b")) isanswer = true; break; case 3: if (subjectitem.answertextcontent.Contains("C") || subjectitem.answertextcontent.Contains("c")) isanswer = true; break; case 4: if (subjectitem.answertextcontent.Contains("D") || subjectitem.answertextcontent.Contains("d")) isanswer = true; break; case 5: if (subjectitem.answertextcontent.Contains("E") || subjectitem.answertextcontent.Contains("e")) isanswer = true; break; case 6: if (subjectitem.answertextcontent.Contains("F") || subjectitem.answertextcontent.Contains("f")) isanswer = true; break; case 7: if (subjectitem.answertextcontent.Contains("G") || subjectitem.answertextcontent.Contains("g")) isanswer = true; break; case 8: if (subjectitem.answertextcontent.Contains("H") || subjectitem.answertextcontent.Contains("h")) isanswer = true; break; case 9: if (subjectitem.answertextcontent.Contains("I") || subjectitem.answertextcontent.Contains("i")) isanswer = true; break; } } return isanswer; }