asp.net mvc上传头像加剪裁功能

简介: 原文:asp.net mvc上传头像加剪裁功能正好项目用到上传+剪裁功能,发上来便于以后使用。 我不能告诉你们其实是从博客园扒的前台代码,哈哈。 前端是jquery+fineuploader+jquery.
原文: asp.net mvc上传头像加剪裁功能

正好项目用到上传+剪裁功能,发上来便于以后使用。

我不能告诉你们其实是从博客园扒的前台代码,哈哈。

前端是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 });
            }
        }

 

代码不全,这里是源码:下载

目录
相关文章
|
14天前
|
存储 文字识别 C#
.NET开源免费、功能强大的 Windows 截图录屏神器
今天大姚给大家分享一款.NET开源免费(基于GPL3.0开源协议)、功能强大、简洁灵活的 Windows 截图、录屏、Gif动图制作神器:ShareX。
|
3月前
|
开发框架 前端开发 .NET
ASP.NET CORE 3.1 MVC“指定的网络名不再可用\企图在不存在的网络连接上进行操作”的问题解决过程
ASP.NET CORE 3.1 MVC“指定的网络名不再可用\企图在不存在的网络连接上进行操作”的问题解决过程
41 0
|
1月前
|
开发框架 前端开发 .NET
进入ASP .net mvc的世界
进入ASP .net mvc的世界
29 0
|
1月前
mvc.net分页查询案例——mvc-paper.css
mvc.net分页查询案例——mvc-paper.css
5 0
|
1月前
|
Windows
windows server 2019 安装NET Framework 3.5失败,提示:“安装一个或多个角色、角色服务或功能失败” 解决方案
windows server 2019 安装NET Framework 3.5失败,提示:“安装一个或多个角色、角色服务或功能失败” 解决方案
100 0
|
1月前
|
开发框架 前端开发 .NET
C# .NET面试系列六:ASP.NET MVC
<h2>ASP.NET MVC #### 1. MVC 中的 TempData\ViewBag\ViewData 区别? 在ASP.NET MVC中,TempData、ViewBag 和 ViewData 都是用于在控制器和视图之间传递数据的机制,但它们有一些区别。 <b>TempData:</b> 1、生命周期 ```c# TempData 的生命周期是短暂的,数据只在当前请求和下一次请求之间有效。一旦数据被读取,它就会被标记为已读,下一次请求时就会被清除。 ``` 2、用途 ```c# 主要用于在两个动作之间传递数据,例如在一个动作中设置 TempData,然后在重定向到另
95 5
|
2月前
|
C# Windows
.NET开源的一个小而快并且功能强大的 Windows 动态桌面软件
.NET开源的一个小而快并且功能强大的 Windows 动态桌面软件
|
3月前
|
XML 前端开发 定位技术
C#(NET Core3.1 MVC)生成站点地图(sitemap.xml)
C#(NET Core3.1 MVC)生成站点地图(sitemap.xml)
25 0
|
3月前
|
前端开发
.net core mvc获取IP地址和IP所在地(其实是百度的)
.net core mvc获取IP地址和IP所在地(其实是百度的)
123 0
|
5月前
|
开发框架 自然语言处理 前端开发
基于ASP.NET MVC开发的、开源的个人博客系统
基于ASP.NET MVC开发的、开源的个人博客系统
52 0

相关实验场景

更多