在我们使用QQ上传头像,注册用户账号时是不是都会遇到上传图像,并根据自己的要求对图像进行裁剪,这是怎么实现的呐?
本文主要介绍了Asp.net mvc实现上传头像加剪裁功能,分享给大家供大家参考。具体如下:
运行效果截图如下:
具体代码如下:
前台代码
<link href="https://www.jb51.net/~/Content/fineuploader.css" /> <link href="https://www.jb51.net/~/Content/jquery.Jcrop.min.css" /> <link href="https://www.jb51.net/~/Content/crop.min.css" /> <script src="https://www.jb51.net/~/Scripts/jquery-1.8.2.min.js"></script> <script src="https://www.jb51.net/~/Scripts/jquery.fineuploader-3.1.min.js"></script> <script src="https://www.jb51.net/~/Scripts/jquery.Jcrop.min.js"></script> <script src="https://www.jb51.net/~/Scripts/crop.js"></script> <div></div> <div></div> <div> <div> <div> <img alt="" src="" /> </div> <div> <div>当前头像</div> <div>180px × 180px</div> <div> <img alt="" src="https://www.jb51.net/@ViewBag.Path"/></div> </div> </div> <div> <form action="/home/index" method="post"> <input type="hidden"> <input type="hidden"> <input type="hidden"> <input type="hidden"> <input type="hidden"> <input type="submit" value="裁切并保存" /><span></span> </form> </div> <div></div> </div>
后台代码
public ActionResult Index() { return View(); } /// <summary> /// 保存缩略图 /// </summary> /// <param></param> /// <returns></returns> [HttpPost] public ActionResult Index(FormCollection form) { int x = Convert.ToInt32(form["x"]); int y = Convert.ToInt32(form["y"]); int w = Convert.ToInt32(form["w"]); int h = Convert.ToInt32(form["h"]); string imgsrc = form["imgsrc"].Substring(0, form["imgsrc"].LastIndexOf("?")); string path = ImgHandler.CutAvatar(imgsrc, x, y, w, h); //保存Path ViewBag.Path = path; return View(); } /// <summary> /// 上传头像 /// </summary> /// <param></param> /// <returns></returns> [HttpPost] public ActionResult ProcessUpload(string qqfile) { try { string uploadFolder = "/Upload/original/" + DateTime.Now.ToString("yyyyMM") + "https://www.jb51.net/"; string imgName = DateTime.Now.ToString("ddHHmmssff"); string imgType = qqfile.Substring(qqfile.LastIndexOf(".")); string uploadPath = ""; uploadPath = Server.MapPath(uploadFolder); if (!Directory.Exists(uploadPath)) { Directory.CreateDirectory(uploadPath); } using (var inputStream = Request.InputStream) { using (var flieStream = new FileStream(uploadPath + imgName + imgType, FileMode.Create)) { inputStream.CopyTo(flieStream); } } return Json(new { success = true, message = uploadFolder + imgName + imgType }); } catch (Exception e) { return Json(new { fail = true, message = e.Message }); } }
以上就是实现Asp.net mvc上传头像加剪裁功能的部分代码,小编分享给大家参考,希望对大家的学习有所帮助。
您可能感兴趣的文章: