/// <summary> /// 执行带文件上传的HTTP POST请求。 /// </summary> /// <param name="url">请求地址</param> /// <param name="textParams">请求文本参数</param> /// <param name="fileParams">请求文件参数</param> /// <returns>HTTP响应</returns> public string DoPost(string url, IDictionary <string, string> textParams, IDictionary <string, HttpPostedFile> fileParams) { // 如果没有文件参数,则走普通POST请求 if (fileParams == null || fileParams.Count == 0) { return(DoPost(url, textParams)); } string boundary = DateTime.Now.Ticks.ToString("X"); // 随机分隔线 HttpWebRequest req = GetWebRequest(url, "POST"); req.ContentType = "multipart/form-data;charset=utf-8;boundary=" + boundary; System.IO.Stream reqStream = req.GetRequestStream(); byte[] itemBoundaryBytes = Encoding.UTF8.GetBytes("\r\n--" + boundary + "\r\n"); byte[] endBoundaryBytes = Encoding.UTF8.GetBytes("\r\n--" + boundary + "--\r\n"); // 组装文本请求参数 string textTemplate = "Content-Disposition:form-data;name=\"{0}\"\r\nContent-Type:text/plain\r\n\r\n{1}"; IEnumerator <KeyValuePair <string, string> > textEnum = textParams.GetEnumerator(); while (textEnum.MoveNext()) { string textEntry = string.Format(textTemplate, textEnum.Current.Key, textEnum.Current.Value); byte[] itemBytes = Encoding.UTF8.GetBytes(textEntry); reqStream.Write(itemBoundaryBytes, 0, itemBoundaryBytes.Length); reqStream.Write(itemBytes, 0, itemBytes.Length); } // 组装文件请求参数 string fileTemplate = "Content-Disposition:form-data;name=\"{0}\";filename=\"{1}\"\r\nContent-Type:{2}\r\n\r\n"; IEnumerator <KeyValuePair <string, HttpPostedFile> > fileEnum = fileParams.GetEnumerator(); while (fileEnum.MoveNext()) { string key = fileEnum.Current.Key; HttpPostedFile fileItem = fileEnum.Current.Value; string fileEntry = string.Format(fileTemplate, key, fileItem.FileName, fileItem.GetType()); byte[] itemBytes = Encoding.UTF8.GetBytes(fileEntry); reqStream.Write(itemBoundaryBytes, 0, itemBoundaryBytes.Length); reqStream.Write(itemBytes, 0, itemBytes.Length); byte[] fileBytes = new byte[fileItem.ContentLength]; using (System.IO.Stream fileStream = fileItem.InputStream) { byte[] Bytes = new byte[fileItem.ContentLength]; fileStream.Read(Bytes, 0, Bytes.Length); fileBytes = Bytes; } reqStream.Write(fileBytes, 0, fileBytes.Length); } reqStream.Write(endBoundaryBytes, 0, endBoundaryBytes.Length); reqStream.Close(); HttpWebResponse rsp = (HttpWebResponse)req.GetResponse(); Encoding encoding = Encoding.GetEncoding(rsp.CharacterSet); return(GetResponseAsString(rsp, encoding)); }
public void updateTeacher() { try { Teacher Teacher = new Teacher(); setValue(Teacher, context); HttpPostedFile hpf = context.Request.Files["headImgFile"]; if (hpf != null) { string savepath = context.Server.MapPath("/uploadFile/headImg/" + Teacher.Id + "." + hpf.GetType()); //路径,相对于服务器当前的路径 hpf.SaveAs(savepath); //保存 Teacher.HeadImage = savepath; } TeacherService s = new TeacherService(); s.save(Teacher); context.Response.Write("1"); } catch (Exception e) { context.Response.Write("0"); } }
public void updateStudent() { try { Student student = new Student(); setValue(student, context); HttpPostedFile hpf = context.Request.Files["headImgFile"]; if (hpf != null) { string savepath = context.Server.MapPath("/uploadFile/headImg/" + student.Id + "." + hpf.GetType()); //路径,相对于服务器当前的路径 hpf.SaveAs(savepath); //保存 student.HeadImage = savepath; } DepartmentService ds = new DepartmentService(); string professionID = context.Request.Form.Get("Profession"); if (!string.IsNullOrEmpty(professionID)) { Profession profession = ds.getProfessionByID(professionID); if (profession != null) { student.Profession = profession; } } string ClassGradeID = context.Request.Form.Get("ClassGrade"); if (!string.IsNullOrEmpty(ClassGradeID)) { ClassGrade classGrade = ds.getClassGradeByID(ClassGradeID); if (classGrade != null) { student.ClassGrade = classGrade; } } StudentService s = new StudentService(); s.save(student); context.Response.Write("1"); } catch (Exception e) { context.Response.Write("0"); } }