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基础 response.read 输出网页的源代码

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

  2. ng-model绑定的是ng-option中的什么?

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <script sr ...

  3. hihoCoder week6 01背包

    01背包 题目链接 https://hihocoder.com/contest/hiho6/problem/1 #include <bits/stdc++.h> using namespa ...

  4. How to know the directory size in CENTOS 查看文件夹大小

    Under any linux system, you want to use the command du. (Disk Usage) Common usage is : du -sh file(s ...

  5. NLP related basic knowledge with deep learning methods

    NLP related basic knowledge with deep learning methods  2017-06-22   First things first >>> ...

  6. [CodeForces - 276A] Lunch Rush

    题目链接:http://codeforces.com/problemset/problem/276/A 从这n个输入中求最大值,注意 和 k的比较,定义一个maxn,对每个输入进行计算即可. AC代码 ...

  7. Win10下Java开发环境配置

    首先下载符合操作系统版本的jdk,比如64位的JDK8: 下载链接:https://www.oracle.com/technetwork/java/javase/downloads/jdk8-down ...

  8. facebook api之基本概念(中文)

    Facebook广告API系列 1 Facebook Graph API Facebook提供了一套类rest的接口,统称为Graph API.为啥叫Graph?因为facebook把所有的资源都抽象 ...

  9. Sublime Text3 插件:DocBlockr与javascript注释规范

    原:http://www.ithao123.cn/content-719950.html 1.引子 在写代码的时候,尤其是写脚本,最需要注释了.目前脚本.样式的注释格式都有一个已经成文的约定规范(这些 ...

  10. Using keytool to import keystore

    open command line and locate to the location of  keytool.exe. import cert to keystore command: keyto ...