asp.net后台InputStream存储图片,前台js接收解析存放路径地址
///后台代码
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", "<script>GetWaybillDetail('" + Session["waybillID"].ToString() + "','jsServer')</script>"); } } 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('.')); } /// <summary> /// 缩放图片 /// </summary> /// <param name="image">Image 对象</param> /// <param name="width">图片新的宽度</param> /// <param name="height">图片新高度</param> /// <param name="scale">是否按比例缩放图片</param> /// <returns>Image 对象</returns> 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); } }) }

浙公网安备 33010602011771号