您好,登錄后才能下訂單哦!
這篇文章將為大家詳細講解有關如何在Asp.net中使用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" /> </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" /></div> </div> </div> <div id="crop_operation" > <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" 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 { success = true, message = uploadFolder + imgName + imgType }); } catch (Exception e) { return Json(new { fail = true, message = e.Message }); } }
關于如何在Asp.net中使用mvc對上傳頭像進行剪裁就分享到這里了,希望以上內容可以對大家有一定的幫助,可以學到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。