Asp.net mvc上傳頭像加剪裁功能

來源:互聯網
上載者:User

正好項目用到上傳+剪裁功能,發上來便於以後使用。

我不能告訴你們其實是從部落格園扒的前台代碼,哈哈。

前端是jquery+fineuploader+jquery.Jcrop

後台是asp.net mvc 4

核心的js調用代碼是crop.js和helper檔案夾下的ImgHandler.cs

效果圖

前台代碼
<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 { success = true, message = uploadFolder + imgName + imgType });            }            catch (Exception e)            {                return Json(new { fail = true, message = e.Message });            }        }

代碼不全,這裡是源碼:下載



相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.