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

所属分类: 网络编程 / ASP.NET 阅读数: 360
收藏 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上传头像加剪裁功能的部分代码,小编分享给大家参考,希望对大家的学习有所帮助。

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

.NET实现XML与DataTable互转的实例代码

.NET实现XML与DataTable互转的实例代码,需要的朋友可以参考一下
收藏 0 赞 0 分享

使用DataAdapter填充多个表(利用DataRelation)的实例代码

使用DataAdapter填充多个表(利用DataRelation)的实例代码,需要的朋友可以参考一下
收藏 0 赞 0 分享

Repeater全选删除和分页实现思路及代码

Repeater控件想必熟悉.net web开发的人员是很了解不过的了,接下来将与大家共同学习下它的全选删除和分页,感兴趣的你可不要错过了哈,希望可以帮助到你
收藏 0 赞 0 分享

.net搜索查询并实现分页实例

.net搜索查询并实现分页实例,需要的朋友可以参考一下
收藏 0 赞 0 分享

Repeater对数据进行格式化处理

最近不止一个同学,问我在Repeater里怎么格式化数据,怎么处理。因为Repeater 属于服务器端控件。要么利用本身的控件事件来处理,要么在数据源上处理。
收藏 0 赞 0 分享

合并两个DataSet的数据内容的方法

合并两个DataSet的数据内容的方法,需要的朋友可以参考一下
收藏 0 赞 0 分享

.NET运行界面上,实现随意拖动控件的方法

.NET运行界面上,实现随意拖动控件的方法,需要的朋友可以参考一下
收藏 0 赞 0 分享

ASP.NET实现License Key输入功能的小例子

当我们安装微软的软件,多数软件是需要输入license key。它有五个文本框,输入完第一个文本框之后,光标自动跳至下一个文本框。 Insus.NET今天也使用asp.net来模仿一个。呵呵。
收藏 0 赞 0 分享

asp.net中将js的返回值赋给asp.net控件的小例子

要做一个显示用户在线停留时间的功能,拖了一个label控件用于显示时间,而时间是通过js来实现的,现在要把js的返回值赋给label,方法如下:
收藏 0 赞 0 分享

GridView高效分页和搜索功能的实现代码

GridView高效分页和搜索功能的实现代码,需要的朋友可以参考一下
收藏 0 赞 0 分享
查看更多