博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
asp.net后台InputStream存储图片,前台js接收解析存放路径地址
阅读量:4576 次
发布时间:2019-06-08

本文共 4843 字,大约阅读时间需要 16 分钟。

///后台代码 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);                }            })        }

 

转载于:https://www.cnblogs.com/libinbin/p/3392400.html

你可能感兴趣的文章
Eclipse使用技巧
查看>>
网络请求之get与post异步请求
查看>>
堆和栈的区别
查看>>
清理内存
查看>>
蓝桥杯之装箱问题
查看>>
Spark常用算子详解
查看>>
JAVA_桥接模式
查看>>
C语言 strcpy,memcpy,memmove,memccpy函数
查看>>
C语言一个小程序的bug疑问 数组相关[已解决]
查看>>
几种排序算法(PHP版)
查看>>
数据库字段数据类型对索引的影响
查看>>
mesos cluster
查看>>
Altium Designer 中差分走线
查看>>
linux 解压缩命令
查看>>
GDUT校赛
查看>>
(HDU)1076 --An Easy Task(简单任务)
查看>>
团队精神与集体主义的区别?
查看>>
Spring Boot 入门(Spring Cloud方向)
查看>>
AngularJS(九):路由
查看>>
GPS.NET 和 GeoFramework开源了
查看>>