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

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

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

ASP.NET笔记之 控件与母板的区别分析

本篇文章小编为大家介绍,ASP.NET笔记之 控件与母板的区别分析。需要的朋友参考下
收藏 0 赞 0 分享

.NET动态加载用户控件并传值的方法

.NET动态加载用户控件并传值的方法,有需要的朋友可以参考一下
收藏 0 赞 0 分享

扩展ASP.NET MVC三层框架且使用StructureMap实现依赖注入1-Model层

本篇文章将向大家介绍如何添加Service和Repository层并且使用StructureMap把Service层注入到Controller,把Repository注入到Service层。
收藏 0 赞 0 分享

aspnetpager重写url(伪静态)配置实例

这几天要用到AspNetPager来做伪静态分页,找了些资料并把修改过程记录下来。
收藏 0 赞 0 分享

C#实现支持断点续传多线程下载客户端工具类

C#实现支持断点续传多线程下载的 Http Web 客户端工具类 (C# DIY HttpWebClient),感兴趣的朋友可以参考下本文,或许对你有所帮助
收藏 0 赞 0 分享

DataGridView多维表头的实现方法

不过我自己还是扩展了DataGridView,使之能制作出多维表头。
收藏 0 赞 0 分享

gridview+objectdatasource+aspnetpager整合实例

gridview+objectdatasource+aspnetpager整合实例,需要的朋友可以参考一下
收藏 0 赞 0 分享

DataTable多列合并问题轻松搞定

由于题库的表结构不相同,导致同样的Gridview在显示时不能同时两种不同结构的数据,这时如何在这个固定的GridView中显示不同的数据呢,感兴趣的朋友可以看下本文的解决方法
收藏 0 赞 0 分享

Asp.net静态方法之Grid转DataTable方法实现步骤

GridView绑定DataTable后,如何获取GridView绑定后显示的值,在项目需求的背景下写了一个静态方法,经过在项目中的使用,bug的修复,较为稳定
收藏 0 赞 0 分享

Asp.net防重复提交机制实现方法

在Button或其他控件加上下面两个属性:UseSubmitBehavior="false"及OnClientClick设置控件为不可用即可,感兴趣的朋友可以参考下哈
收藏 0 赞 0 分享
查看更多