///后台代码 public Hashtable HtFilePath{get{if (ViewState["HtFilePath"] == null){Hashtable ht = new Hashtable();ht.Add("filefirst", "");ht.Add("filesecond", "");ht.Add("filethird", "");ViewState["HtFilePath"] = ht;}return (Hashtable)ViewState["HtFilePath"];}}protected void Page_Load(object sender, EventArgs e){if (!IsPostBack) {if (Request["waybillID"] != null) {Session["waybillID"] = Request["waybillID"];Session.Timeout = 30;string waybillID = Session["waybillID"].ToString();}if (!string.IsNullOrEmpty(Session["waybillID"].ToString())){this.Page.ClientScript.RegisterStartupScript(this.GetType(), "SayHello", "");}}if (IsPostBack){var sleDutyGenre = Request.Form["sleDutyGenre"];var sleDutyType = Request.Form["sleDutyType"];var sleDutyLink = Request.Form["sleDutyLink"];var txtComplainContent = Request.Form["txtComplainContent"];var txtHiddenWaybillID = Request.Form["txtHiddenWaybillID"];var txtAcceptPeople = Request.Form["txtAcceptPeople"];Hashtable complaint = new Hashtable();complaint.Add("DutyGenre", sleDutyGenre);complaint.Add("DutyType", sleDutyType);complaint.Add("DutyLink", sleDutyLink);complaint.Add("ComplainContent", txtComplainContent);complaint.Add("WaybillID", txtHiddenWaybillID);complaint.Add("AcceptPeople", txtAcceptPeople);complaint.Add("picPath", "");if (!string.IsNullOrEmpty(this.fileFirst.Value)){HttpPostedFile httpPostedFile = this.fileFirst.PostedFile;Image image = new Bitmap(httpPostedFile.InputStream);image = Resize(image, 800, 600, true);string fileName = string.Format("{0}_{1}.jpeg", GetFileName(httpPostedFile.FileName), DateTime.Now.ToString("yyyyMMddhhmmssfff"));string relativeFilePath = ConfigurationManager.AppSettings["QualityFeedbackDir"] + fileName;string saveFilePath = HttpContext.Current.Server.MapPath(relativeFilePath);SaveMap(image, saveFilePath, true);HtFilePath[this.fileUploadName.Value.ToLower()] = relativeFilePath;complaint["picPath"] = relativeFilePath;}string complaints = SerializerUtility.JsonSerializeByJNet(complaint);ClientScript.RegisterStartupScript(this.GetType(), Guid.NewGuid().ToString(), string.Format("setPicturePath('{0}','{1}');", "[" + SerializerUtility.JsonSerializeByJNet(HtFilePath) + "]",complaints), true);}}private string GetFileName(string strFilePath){if (strFilePath == null)return string.Empty;string fileName = strFilePath.Split('\\').ToList().Last();return fileName.Substring(0, fileName.LastIndexOf('.'));}////// 缩放图片 /// /// Image 对象 /// 图片新的宽度 /// 图片新高度 /// 是否按比例缩放图片 ///Image 对象 public Image Resize(Image image, int width, int height, bool scaleable){// 定义图片的新尺寸 int iWidth, iHeight;// 如果是按比例缩放图片(即scaleable = true),生成的图片的尺寸以不超过指定尺寸为准,否则以绝对尺寸为准 if (scaleable){if (image.Width > image.Height){iWidth = width;iHeight = image.Height * iWidth / image.Width;}else{iHeight = height;iWidth = image.Width * iHeight / image.Height;}}else{iWidth = width;iHeight = height;}Rectangle r = new Rectangle(0, 0, iWidth, iHeight);Image img = new Bitmap(iWidth, iHeight);using (Graphics g = Graphics.FromImage(img)){// 定义缩放图片为高质量 g.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighSpeed;//g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighSpeed; g.DrawImage(image, r);}return img;}public string SaveMap(Image image, string filePath, bool compressible){if (compressible){ImageCodecInfo ici = ImageCodecInfo.GetImageEncoders().Where(p => p.MimeType.ToLower() == "image/jpeg").FirstOrDefault();EncoderParameters ep = new EncoderParameters();ep.Param[0] = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, Convert.ToInt32(40)); //这里最后一个参数,需要注意,即使给的参数是整数,也必须要转成Int32,否则会被默认为byte类型!image.Save(filePath, ici, ep);}else{image.Save(filePath);}return filePath;}
前台脚本函数
function setPicturePath(data, cp) { var complaint = eval('(' + cp + ')'); var picPath = complaint.picPath; var complaint = { "DutyGenre": complaint.DutyGenre, "DutyType": complaint.DutyType, "DutyLink": complaint.DutyLink, "ComplainContent": complaint.ComplainContent, "WaybillID": complaint.WaybillID, "ComplainPeople": complaint.ComplainPeople, "AcceptPeople": complaint.AcceptPeople }; $.post("Handler/CctComplaintHandler.ashx?action=CreateComplaint&temp=" + new Date().getTime(), { "complaint": JSON.stringify(complaint), "picPath": picPath }, function (data) { var jsonData = eval(data)[0]; if (jsonData.State == "0") { $.ligerDialog.success(jsonData.Desc, "", function () { }, false); } else { $.ligerDialog.error(jsonData.Desc); } }) }