1、引入的外部资源:

jquery.Jcrop.css、jquery.Jcrop.js、upimg.js

2、使用的页面元素

                    @* 选择照片 *@
<div class="line">
<h2 class="not">请选择照片:</h2>
<div class="">
<input id="image_file" name="updata_file" accept="image/gif,image/jpg,image/bmp,image/jpeg,image/png,image/tiff,image/tif" style="display:none" value="选择照片" type="file" />
<button id="image_filebtn" class="hex-lightgrey-btn" type="button">选择照片</button>
<button id="save_btn" class="hex-green-btn" style="display: none" type="button">确认修改</button>
<span class="hex-font-orange error">仅支持JPG、PNG格式图片,且文件小于2M</span>
</div>
</div>
@* 头像裁剪和预览 *@
<div class="img">
<div id="wrapper" class="hex-wrapper" style="width: 600px;height: 600px">
@if (!string.IsNullOrEmpty(Model.Profile.User.Avatar))
{
<img id="preview" src="@WebTools.ConvertResourceUrl(Model.Profile.User.Avatar)" />
}
else
{
<img id="preview" src="/Content/Images/userdefault.jpg" />
}
</div>
<div>
<h6>头像预览:</h6>
<canvas id="cropper" width="250" height="250"></canvas>
</div>
</div>

3、使用的脚本方法

var cropper;
$(document).ready(function () {
$("#image_file").change(function () {
Loading();
fileSelectHandler();
var errorMsg = $(".error").html();
if (errorMsg != "请选择一个有效的图像文件(JPG和PNG是被允许的)" && errorMsg != "您选择的文件太大,请选择一个较小的图像文件") {
$("#save_btn").show();
}
LoadingEnd();
});
$("#image_filebtn").click(function () {
$("#image_file").click();
}); $("#save_btn").bind("click", function () {
Loading();
var imgUrl = document.getElementById("cropper").toDataURL("image/png");
var imageData = encodeURIComponent(imgUrl);
$.ajax({
url: "/Profile/PhotoHandle",
type: "post",
data: { imageData: imageData },
success: function (data) {
if (data) {
document.getElementById("photo_img").src = imgUrl;
} else {
alertno("删除失败");
LoadingEnd();
}
}
});
});
});

4、control的方法,发送服务器请求

     public ActionResult PhotoHandle(string imageData)
{
string imgStr = HttpUtility.UrlDecode(imageData, Encoding.UTF8).Split(',')[];
byte[] imageBytes = Convert.FromBase64String(imgStr);
string result = SaveImage(imageBytes, CurrentUser.Name);
var obj = Common.Serialization.JsonHelper.JsonToObject<UpLoadBack>(result);
EntAppFrameWorkContext.DomainModelService.ExtenedService<IUser>()
.UpdateAvatar(Int64.Parse(CurrentUser.Key), obj.ResourceID.ToString());return Content(obj.ResourceID.ToString());
}
        public static string SaveImage(byte[] imageBytes, string CurrentUserName)
{
string cms = WebConfigHelper.CmsWebsite.Replace("https", "http");
if (string.IsNullOrEmpty(cms)) return null;
var token = Token.GetToken(CurrentUserName);
string url = cms.TrimEnd('/') + "/FileManagement/UpLoadFile_Server?ssotoken=" + token;
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url);
request.Method = "POST";
request.KeepAlive = true;
request.ContentType = "application/x-www-form-urlencoded";
request.Headers.Add("ext", "png");
request.Headers.Add("contentType", "image/png");
request.Headers.Add("hash", imageBytes.GetHashCode().ToString());
//填充POST数据
request.ContentLength = imageBytes.Length;
//myReq.
Stream requestStream = request.GetRequestStream();
requestStream.Write(imageBytes, , imageBytes.Length);
requestStream.Close();
string result = string.Empty;
try
{
//发送POST数据请求服务器
HttpWebResponse HttpWResp = (HttpWebResponse)request.GetResponse(); Stream myStream = HttpWResp.GetResponseStream(); //获取服务器返回信息
StreamReader reader = new StreamReader(myStream, Encoding.UTF8);
result = reader.ReadToEnd(); //释放
myStream.Close(); }
catch (Exception exp)
{
result = "报错:" + exp.Message;
} return result;
}

结合canvas和jquery.Jcrop.js裁切图像上传图片的更多相关文章

  1. 实现图像剪裁 jquery.Jcrop

       配合 jquery.Jcrop 实现上传图片进行剪裁保存功能    <script src="js/jquery.min.js"></script> ...

  2. 基于 jQuery Jcrop 插件的功能模块:头像剪裁

    /** Jcrop version: 0.9.12 jQuery version: 1.9.0 Author: 小dee Date: 2014.10.30 */ 先看看他山之石:博客园的头像模块也是使 ...

  3. jQuery Jcrop API参数说明(中文版)(转)(图片剪切)

    Jcrop是一个jQuery图片裁剪插件,它能为你的WEB应用程序快速简单地提供图片裁剪的功能.特点如下: 对所有图片均unobtrusively(无侵入的,保持DOM简洁) 支持宽高比例锁定 支持 ...

  4. struts2+jsp+jquery+Jcrop实现图片裁剪并上传

    <1> 使用html标签上传需要裁剪的大图. <2> 在页面呈现大图,使用Jcrop(Jquery)对大图进行裁剪,并且可以进行预览. <3> 选择好截取部分之后发 ...

  5. jQuery JCrop插件的使用详解

    jQuery的一个图片剪切的一个插件, 使用插件必须条件:引入jQuery.js文件,引入jQuery.Jcrop.js文件,引入JQuery.Jcrop.css文件   1.最基本的使用方法: &l ...

  6. jQuery Jcrop 图像裁剪

    jQuery Jcrop 图像裁剪 http://code.ciaoca.com/jquery/jcrop/ cropper.js 实现HTML5 裁剪图片并上传(裁剪上传头像.) https://b ...

  7. 调用jquery.Jcrop.min.js 切割图片 实例

    需求是:上传一个图片,然后将上传的这个图片进行切割........ 首先是jsp页面.页面需要引入js  <script src="${fileUrlPrx}/scripts/wap/ ...

  8. js 图片处理 Jcrop.js API

    引入jquery.Jcrop.min.css和jquery.Jcrop.min.js 参数/接口说明 options 参数说明 名称 默认值 说明 allowSelect true 允许新选框 all ...

  9. 动态生成二维码插件 jquery.qrcode.js

    前段时间做项目,需要动态生成一个二维码,于是就在网上找了一下发现一个jquery插件jquery.qrcode.js,所以今天就简单说一下这个插件的使用: jquery.qrcode.js是依赖jqu ...

随机推荐

  1. Python3基础 list [] 创建整数列表

             Python : 3.7.0          OS : Ubuntu 18.04.1 LTS         IDE : PyCharm 2018.2.4       Conda ...

  2. luogu1975 [国家集训队]排队

    思路 序列中 |i | 1| 2| 3| 4| 5| 6| 7| 8| 9| 10| |----|--|--|--|--|--|--|--|--|--|--| |a[i]| a| b| c| L| d ...

  3. dp小结|背包问题

    1.先放上0-1背包模板 二维数组 for(int i=1;i<=n;i++)//枚举 物品 for(int j=1;j<=V;j++)//枚举体积 //这个位置是可以正序枚举的. qwq ...

  4. SQL Server (MSSQLSERVER) 服务因 2148081668 服务性错误而停止。

    https://zhidao.baidu.com/question/151448005.html 具体步骤: 运行-> 输入:“services.msc” ->找到 “SQL Server ...

  5. BZOJ 4159 [Neerc2009]Business Center

    思路 简单的模拟,答案就是\(min\{(\lfloor\frac{d\times n}{u+d}\rfloor+1)\times(u+d)-d\times n\}\) 代码 #include < ...

  6. 题解——洛谷P3812【模板】线性基

    学了下线性基 使用好像并不复杂 打了板子 但是要注意位运算优先级 #include <cstdio> #include <algorithm> #include <cst ...

  7. 论文阅读: End-to-end Learning of Action Detection from Frame Glimpses in Videos

      End-to-End Learning of Action Detection from Frame Glimpses in Videos  CVPR 2016  Motivation:    本 ...

  8. 4-Four-Seeing hands

      ①Several cases have been reported in Russia recently of people who can read and detect colours wit ...

  9. Kubernetes之容器

    Images You create your Docker image and push it to a registry before referring to it in a Kubernetes ...

  10. Qt button和buttons区别

    假设我的鼠标左键已经按下.若移动鼠标,会发生的move事件,button返回Qt::NoButton,buttons返回LeftButton.再按下右键,会发生press事件,button返回Righ ...