网络编程 
首页 > 网络编程 > 浏览文章

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

(编辑:jimmy 日期: 2024/5/9 浏览:3 次 )

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

本文主要介绍了Asp.net mvc实现上传头像加剪裁功能,分享给大家供大家参考。具体如下: 

运行效果截图如下:

实现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="/UploadFiles/2021-04-02/jquery-1.8.2.min.js">

后台代码

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 MVC3网站创建与发布(1)
下一篇:ASP.NET中Cookie的用法实例分析
高通与谷歌联手!首款骁龙PC优化Chrome浏览器发布
高通和谷歌日前宣布,推出首次面向搭载骁龙的Windows PC的优化版Chrome浏览器。
在对骁龙X Elite参考设计的初步测试中,全新的Chrome浏览器在Speedometer 2.1基准测试中实现了显著的性能提升。
预计在2024年年中之前,搭载骁龙X Elite计算平台的PC将面世。该浏览器的提前问世,有助于骁龙PC问世就获得满血表现。
谷歌高级副总裁Hiroshi Lockheimer表示,此次与高通的合作将有助于确保Chrome用户在当前ARM兼容的PC上获得最佳的浏览体验。
友情链接:杰晶网络 DDR爱好者之家 南强小屋 黑松山资源网 白云城资源网