/// <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> /// 试卷内容对象序列化成xml结构数据 /// </summary> /// <param name="paper"></param> private static string PaperSerializerXml(Paper paper) { XmlSerializer xmls = new XmlSerializer(typeof(Paper)); StringWriter sw = new StringWriter(); xmls.Serialize(sw, paper); XmlDocument xmldoc = new XmlDocument(); xmldoc.LoadXml(sw.ToString()); string xmlpath = System.Windows.Forms.Application.StartupPath + HtmlUtil.paperbak + paper.papername + ".xml"; if (!Directory.Exists(System.Windows.Forms.Application.StartupPath + HtmlUtil.paperbak)) { Directory.CreateDirectory(System.Windows.Forms.Application.StartupPath + HtmlUtil.paperbak); } xmldoc.Save(xmlpath); return sw.ToString(); }
/// <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> /// 试卷内容对象序列化成json结构数据 /// </summary> /// <param name="paper"></param> private static string PaperSerializerJson(Paper paper) { DataContractJsonSerializer json = new DataContractJsonSerializer(paper.GetType()); string szJson = ""; //序列化 using (MemoryStream stream = new MemoryStream()) { json.WriteObject(stream, paper); szJson = Encoding.UTF8.GetString(stream.ToArray()); } return szJson; }