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

具體代碼如下:
前臺(tái)代碼
<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">當(dāng)前頭像</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>后臺(tái)代碼
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 });
}
}
以上就是實(shí)現(xiàn)Asp.net mvc上傳頭像加剪裁功能的部分代碼,小編分享給大家參考,希望對(duì)大家的學(xué)習(xí)有所幫助。
相關(guān)文章
教你30分鐘通過Kong實(shí)現(xiàn).NET網(wǎng)關(guān)
Kong是一個(gè)Openrestry程序,而Openrestry運(yùn)行在Nginx上,用Lua擴(kuò)展了nginx。所以可以認(rèn)為Kong = Openrestry + nginx + lua,這篇文章主要介紹了30分鐘通過Kong實(shí)現(xiàn).NET網(wǎng)關(guān),需要的朋友可以參考下2021-11-11
ASP.NET?Core?模型驗(yàn)證消息的本地化新姿勢(shì)詳解
文章介紹了如何在ASP.NET Core中對(duì)模型驗(yàn)證消息進(jìn)行本地化,默認(rèn)情況下,驗(yàn)證消息是英文的,不方便用戶理解,通過自定義資源類并將其放入項(xiàng)目中,可以實(shí)現(xiàn)驗(yàn)證消息的本地化,這樣無需為每個(gè)模型手動(dòng)指定錯(cuò)誤消息,提升了開發(fā)效率,感興趣的朋友一起看看吧2025-03-03
ASP.NET Core AutoWrapper 自定義響應(yīng)輸出實(shí)現(xiàn)
這篇文章主要介紹了ASP.NET Core AutoWrapper 自定義響應(yīng)輸出實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-08-08
ASP.NET Core中使用MialKit實(shí)現(xiàn)郵件發(fā)送功能
這篇文章主要介紹了ASP.NET Core中使用MialKit實(shí)現(xiàn)郵件發(fā)送功能,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-10-10
Repeater控件動(dòng)態(tài)變更列(Header,Item和Foot)信息(重構(gòu)cs)
上一篇雖然它算不上是完全動(dòng)態(tài)化,但它已經(jīng)達(dá)到初期想要的效果,現(xiàn)另開一篇,不是重新另外寫,而是想重構(gòu)cs的代碼,因?yàn)榍耙黄拇a雖然簡(jiǎn)單,但代碼冗余過多,感興趣的朋友可以參考下哈2013-03-03
動(dòng)態(tài)加載用戶控件至DataList并為用戶控件賦值實(shí)例演示
本文借用使用通用的新聞例子演示動(dòng)態(tài)加載用戶控件至DataList并為用戶控件賦值,感興趣的朋友可以了解下2013-01-01

