Asp.net?mvc實現(xiàn)上傳頭像加剪裁功能
在我們使用QQ上傳頭像,注冊用戶賬號時是不是都會遇到上傳圖像,并根據(jù)自己的要求對圖像進行裁剪,這是怎么實現(xiàn)的吶?
本文主要介紹了Asp.net mvc實現(xiàn)上傳頭像加剪裁功能,分享給大家供大家參考。具體如下:
運行效果截圖如下:

具體代碼如下:
前臺代碼
<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 });
}
}
以上就是實現(xiàn)Asp.net mvc上傳頭像加剪裁功能的部分代碼,小編分享給大家參考,希望對大家的學習有所幫助。
相關(guān)文章
教你30分鐘通過Kong實現(xiàn).NET網(wǎng)關(guān)
Kong是一個Openrestry程序,而Openrestry運行在Nginx上,用Lua擴展了nginx。所以可以認為Kong = Openrestry + nginx + lua,這篇文章主要介紹了30分鐘通過Kong實現(xiàn).NET網(wǎng)關(guān),需要的朋友可以參考下2021-11-11
ASP.NET Core AutoWrapper 自定義響應輸出實現(xiàn)
這篇文章主要介紹了ASP.NET Core AutoWrapper 自定義響應輸出實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-08-08
ASP.NET Core中使用MialKit實現(xiàn)郵件發(fā)送功能
這篇文章主要介紹了ASP.NET Core中使用MialKit實現(xiàn)郵件發(fā)送功能,本文給大家介紹的非常詳細,具有一定的參考借鑒價值,需要的朋友可以參考下2019-10-10
Repeater控件動態(tài)變更列(Header,Item和Foot)信息(重構(gòu)cs)
上一篇雖然它算不上是完全動態(tài)化,但它已經(jīng)達到初期想要的效果,現(xiàn)另開一篇,不是重新另外寫,而是想重構(gòu)cs的代碼,因為前一篇的代碼雖然簡單,但代碼冗余過多,感興趣的朋友可以參考下哈2013-03-03
動態(tài)加載用戶控件至DataList并為用戶控件賦值實例演示
本文借用使用通用的新聞例子演示動態(tài)加載用戶控件至DataList并為用戶控件賦值,感興趣的朋友可以了解下2013-01-01

