Asp.net mvc实现上传头像加剪裁功能

所属分类: 网络编程 / ASP.NET 阅读数: 352
收藏 0 赞 0 分享

在我们使用QQ上传头像,注册用户账号时是不是都会遇到上传图像,并根据自己的要求对图像进行裁剪,这是怎么实现的呐?

本文主要介绍了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" 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 });
   }
  }

以上就是实现Asp.net mvc上传头像加剪裁功能的部分代码,小编分享给大家参考,希望对大家的学习有所帮助。

更多精彩内容其他人还在看

C#中Dictionary几种遍历的实现代码

C#中Dictionary几种遍历的实现代码,需要的朋友可以参考一下
收藏 0 赞 0 分享

C#实现EXCEL数据到TXT文档的转换

C#实现EXCEL数据到TXT文档的转换,需要的朋友可以参考一下
收藏 0 赞 0 分享

C#数据绑定控件中的DataSource属性浅谈

使用该属性指定用来填充Repeater控件的数据源。DataSource可以是任何System.Collections.IEnumerable对象, 如用于访问数据库的System.Data.DataView、System.Collections.ArrayList、Syste
收藏 0 赞 0 分享

ASP.Net开发常见的一些问题总结

ASP.Net开发常见的一些问题总结,需要的朋友可以参考一下
收藏 0 赞 0 分享

Log4net日志记录组件的使用步骤详解和下载

Log4net日志记录组件的使用步骤详解,需要的朋友可以参考一下
收藏 0 赞 0 分享

ASP.NET主机资源控制的一些心得

您可以通过以下设置控制ASP.NET主机对服务器内存的占用。并能设置ASP.NET主机进程定时重建这样可以避免服务器长时间运行aspnet占用大量空闲内存,有利于提高aspnet运行效率。
收藏 0 赞 0 分享

ADO与ADO.NET的区别与介绍

ADO与ADO.NET简介ADO与ADO.NET既有相似也有区别
收藏 0 赞 0 分享

Asp.Net各种超时问题总结

在数据库或者请求操作时,如果选择的时间段过短或操作数据量过大,就会遇到"请求超时"的的问题,网络上提供很多解决方案,但普遍不完善,根据个人经验及参考网络解决方案,先将其汇总
收藏 0 赞 0 分享

vs.net2008添加模板方法

vs.net2008添加模板方法,需要的朋友可以参考一下
收藏 0 赞 0 分享

Asp.net后台把脚本样式输出到head标签中节省代码冗余

最近在学习开发服务器控件,其它就少不了为控件注册js和css之类的资源文件,或者直接注册纯脚本样式。其中就遇到如下问题     1、 注册的资源文件或纯脚本样式在生成的页面中都不在head标签中(当然这个不影响页面功能)     2、 一个页面使用多个一样的控件时,会
收藏 0 赞 0 分享
查看更多