实现Asp.net mvc上传头像加剪裁功能
网络编程 2021-07-04 22:41www.168986.cn编程入门
这篇文章主要介绍了实现Asp. mvc上传头像加剪裁功能,需要的朋友可以参考下
在我们使用QQ上传头像,注册用户账号时是不是都会遇到上传图像,并根据自己的要求对图像进行裁剪,这是怎么实现的呐?
本文主要介绍了Asp. mvc实现上传头像加剪裁功能,分享给大家供大家参考。具体如下
运行效果截图如下
具体代码如下
前台代码
<link href="~/Content/fineuploader.css" rel="stylesheet" /> <link href="~/Content/jquery.Jcrop.min.css" rel="stylesheet" /> <link href="~/Content/crop.min.css" rel="stylesheet" /> <script src="~/Scripts/jquery-1.8.2.min.js"></script> <script src="~/Scripts/jquery.fineuploader-3.1.min.js"></script> <script src="~/Scripts/jquery.Jcrop.min.js"></script> <script src="~/Scripts/crop.js"></script> <div id="jquery-wrapped-fine-uploader"></div> <div id="message"></div> <div id="crop_wrap"> <div id="crop_holder"> <div id="crop_area" class="border"> <img id="crop_image" alt="" src="" class="preview-image" style="display: none" /> </div> <div id="preview_area"> <div id="preview_title">当前头像</div> <div id="preview_large_text" class="preview-text">180px × 180px</div> <div id="preview_large_wrap" class="border"> <img id="preview_large" alt="" src="@ViewBag.Path" class="preview-image" style=""/></div> </div> </div> <div id="crop_operation" style="display: none;"> <form id="form_crop" action="/home/index" method="post"> <input type="hidden" name="x" id="x"> <input type="hidden" name="y" id="y"> <input type="hidden" name="w" id="w"> <input type="hidden" name="h" id="h"> <input type="hidden" name="imgsrc" id="imgsrc"> <input id="crop_operation_submit" type="submit" value="裁切并保存" /><span id="crop_operation_msg" style="display: none" class="green"></span> </form> </div> <div id="croped_message" class="green"></div> </div>
后台代码
public ActionResult Index() { return View(); } /// <summary> /// 保存缩略图 /// </summary> /// <param name="form"></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 name="qqfile"></param> /// <returns></returns> [HttpPost] public ActionResult ProcessUpload(string qqfile) { try { string uploadFolder = "/Upload/original/" + DateTime.Now.ToString("yyyyMM") + "/"; 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 { suess = true, message = uploadFolder + imgName + imgType }); } catch (Exception e) { return Json(new { fail = true, message = e.Message }); } }
以上就是实现Asp. mvc上传头像加剪裁功能的部分代码,长沙网络推广分享给大家参考,希望对大家的学习有所帮助。
编程语言
- 如何快速学会编程 如何快速学会ug编程
- 免费学编程的app 推荐12个免费学编程的好网站
- 电脑怎么编程:电脑怎么编程网咯游戏菜单图标
- 如何写代码新手教学 如何写代码新手教学手机
- 基础编程入门教程视频 基础编程入门教程视频华
- 编程演示:编程演示浦丰投针过程
- 乐高编程加盟 乐高积木编程加盟
- 跟我学plc编程 plc编程自学入门视频教程
- ug编程成航林总 ug编程实战视频
- 孩子学编程的好处和坏处
- 初学者学编程该从哪里开始 新手学编程从哪里入
- 慢走丝编程 慢走丝编程难学吗
- 国内十强少儿编程机构 中国少儿编程机构十强有
- 成人计算机速成培训班 成人计算机速成培训班办
- 孩子学编程网上课程哪家好 儿童学编程比较好的
- 代码编程教学入门软件 代码编程教程