使用jquery插件uploadfive、jcrop实现头像上传
1、html页面部分代码:(实现选着图片时,jcrop能够刷新图片)
<script type="text/javascript">
$(function(){
$("#upload_org_code")
.uploadifive(
{
'queueID' : 'some_file_queue',//绑定到隐藏的div,使进度条消失。
'height' : 27,
'width' : 80,
'fileType' :'image',
'buttonText' : '选择图片',
//'swf' : '${pageContext.request.contextPath}/js/upload/uploadify.swf',
'uploadScript' : '${pageContext.request.contextPath}/uploadAvatar.do',
'fileObjName' : 'file',
'auto' : true,
'method' : 'post',
'multi' : false,
'cancelImg' : '${pageContext.request.contextPath}/js/upload/uploadify-cancel.png',
'fileTypeExts' : '*.jpg;*.png',
'fileSizeLimit' : '2MB',
'onUploadComplete' : function(file, data, response) {
//jcrop
$("#target").removeAttr("src");
$("#target")
.attr(
"src",
"${pageContext.request.contextPath}/uploadAvatar/"
+ strToJson(data).fileAvatarUrl);
$("#cutImageAvater").css("display","block");
displayJcrop();
//jcrop preview
$("#newpicture").removeAttr("src");
$("#newpicture")
.attr(
"src",
"${pageContext.request.contextPath}/uploadAvatar/"
+ strToJson(data).fileAvatarUrl);
},
//加上此句会重写onSelectError方法【需要重写的事件】
'overrideEvents' : [ 'onSelectError',
'onDialogClose' ],
//返回一个错误,选择文件的时候触发
'onError' : function(file, fileType,
data) {
alert("图片类型错误或者图片太大!");
}
});
});
//将字符串转换成json格式
function strToJson(data) {
var a = JSON.parse(data);
return a;
}
</script>
<script type="text/javascript">
var jcrop_api,boundx, boundy;//定义全局变量实现jcrop刷新图片的功能。
function displayJcrop(){
var $preview = $('#preview-pane'),
$pcnt = $('#preview-pane .preview-container'),
$pimg = $('#preview-pane .preview-container img'),
xsize = $pcnt.width(),
ysize = $pcnt.height();
// Create variables (in this scope) to hold the API and image size
// var jcrop_api,boundx, boundy,
// Grab some information about the preview pane
// destroy Jcrop if it is existed
if (typeof jcrop_api != 'undefined') //实现jcrop刷新图片的功能
jcrop_api.destroy();
boundx=null;boundy=null;jcrop_api=null;
$('#target').Jcrop({
onChange: updatePreview,
onSelect: updatePreview,
bgOpacity: .4,
setSelect: [ 100, 100, 50, 50 ],
aspectRatio: 1
},function(){
// Use the API to get the real image size
var bounds = this.getBounds();
boundx = bounds[0];
boundy = bounds[1];
// Store the API in the jcrop_api variable
jcrop_api = this;
// Move the preview into the jcrop container for css positioning
$preview.appendto(null);//释放预览图片的空间,使预览图片刷新。
$preview.appendTo(jcrop_api.ui.holder);
});
function updatePreview(c)
{
var x=c.x;
var y=c.y;
var w=c.w;
var h=c.h;
$("#x1").val(parseInt(x));
$("#y1").val(parseInt(y));
$("#w").val(parseInt(w));
$("#h").val(parseInt(h));
if (parseInt(c.w) > 0)
{
var rx = xsize / c.w;
var ry = ysize / c.h;
$pimg.css({
width: Math.round(rx * boundx) + 'px',
height: Math.round(ry * boundy) + 'px',
marginLeft: '-' + Math.round(rx * c.x) + 'px',
marginTop: '-' + Math.round(ry * c.y) + 'px'
});
}
};
}
</script>
<%@ page language="java" contentType="text/html; charset=utf-8"pageEncoding="utf-8"%><html><head><title>头像上传</title><!-- 在裁剪图片页面中,引入两个js文件,和1个Jcrop需要的css文件(Jcrop包中有,注意引入顺序,先引入jQuery --><link rel="stylesheet" type="text/css"href="../css/upload/uploadifive.css"><link rel="stylesheet" type="text/css"href="../css/upload/jquery.Jcrop.min.css" /><link rel="stylesheet" type="text/css"href="../css/css/bootstrap.min.css" /><script type="text/JavaScript" src="../js/upload/jquery.min.js"></script><script type="text/javascript" src="../js/upload/jquery.uploadifive.min.js"></script><script type="text/javascript" src="../js/upload/jquery.Jcrop.min.js"></script><script type="text/javascript" src="../js/upload/jquery.color.js"></script><script type="text/javascript" src="../js/bootstrap.min.js"></script><style type="text/css">#some_file_queue {display: none;//绑定到隐藏的div,使进度条消失。}body { background-color: #d0e4fe;}</style><script type="text/javascript">$(function(){$("#upload_org_code").uploadifive({'queueID' : 'some_file_queue',//绑定到隐藏的div,使进度条消失。'height' : 27,'width' : 80,'fileType' :'image', 'buttonText' : '选择图片',//'swf' : '${pageContext.request.contextPath}/js/upload/uploadify.swf','uploadScript' : '${pageContext.request.contextPath}/uploadAvatar.do','fileObjName' : 'file','auto' : true,'method' : 'post','multi' : false,'cancelImg' : '${pageContext.request.contextPath}/js/upload/uploadify-cancel.png','fileTypeExts' : '*.jpg;*.png','fileSizeLimit' : '2MB',
'onUploadComplete' : function(file, data, response) {//jcrop$("#target").removeAttr("src");$("#target").attr("src","${pageContext.request.contextPath}/uploadAvatar/"+ strToJson(data).fileAvatarUrl);$("#cutImageAvater").css("display","block");displayJcrop();//jcrop preview$("#newpicture").removeAttr("src");$("#newpicture").attr("src","${pageContext.request.contextPath}/uploadAvatar/"+ strToJson(data).fileAvatarUrl);
},//加上此句会重写onSelectError方法【需要重写的事件】'overrideEvents' : [ 'onSelectError','onDialogClose' ],//返回一个错误,选择文件的时候触发'onError' : function(file, fileType,data) {alert("图片类型错误或者图片太大!");}});});//将字符串转换成json格式function strToJson(data) {var a = JSON.parse(data);return a;}</script>
<script type="text/javascript">var jcrop_api,boundx, boundy;//定义全局变量实现jcrop刷新图片的功能。
function displayJcrop(){ var $preview = $('#preview-pane'), $pcnt = $('#preview-pane .preview-container'), $pimg = $('#preview-pane .preview-container img'),
xsize = $pcnt.width(), ysize = $pcnt.height(); // Create variables (in this scope) to hold the API and image size // var jcrop_api,boundx, boundy,
// Grab some information about the preview pane // destroy Jcrop if it is existed if (typeof jcrop_api != 'undefined') //实现jcrop刷新图片的功能 jcrop_api.destroy(); boundx=null;boundy=null;jcrop_api=null; $('#target').Jcrop({ onChange: updatePreview, onSelect: updatePreview, bgOpacity: .4, setSelect: [ 100, 100, 50, 50 ], aspectRatio: 1 },function(){ // Use the API to get the real image size var bounds = this.getBounds(); boundx = bounds[0]; boundy = bounds[1]; // Store the API in the jcrop_api variable jcrop_api = this;
// Move the preview into the jcrop container for css positioning $preview.appendto(null);//释放预览图片的空间,使预览图片刷新。 $preview.appendTo(jcrop_api.ui.holder); }); function updatePreview(c) {var x=c.x; var y=c.y; var w=c.w; var h=c.h; $("#x1").val(parseInt(x)); $("#y1").val(parseInt(y)); $("#w").val(parseInt(w)); $("#h").val(parseInt(h)); if (parseInt(c.w) > 0) { var rx = xsize / c.w; var ry = ysize / c.h;
$pimg.css({ width: Math.round(rx * boundx) + 'px', height: Math.round(ry * boundy) + 'px', marginLeft: '-' + Math.round(rx * c.x) + 'px', marginTop: '-' + Math.round(ry * c.y) + 'px' }); } };
}</script><script type="text/javascript">function goBack(){window.history.back();}</script><style type="text/css">
/* Apply these styles only when #preview-pane has been placed within the Jcrop widget right: -280px; */ #preview-pane { display: block; position: absolute; z-index: 2000; top: 150px; left:450px; padding: 6px; border: 1px rgba(0,0,0,.4) solid; background-color: white;
-webkit-border-radius: 6px; -moz-border-radius: 6px; border-radius: 6px;
-webkit-box-shadow: 1px 1px 5px 2px rgba(0, 0, 0, 0.2); -moz-box-shadow: 1px 1px 5px 2px rgba(0, 0, 0, 0.2); box-shadow: 1px 1px 5px 2px rgba(0, 0, 0, 0.2);}
/* The Javascript code will set the aspect ratio of the crop area based on the size of the thumbnail preview, specified here */#preview-pane .preview-container { width: 250px; height: 170px; overflow: hidden;}.inputAxiesClass{border-radius: 6px;}</style>
</head><body style="background-image: url(../images/section9_.jpg)"><div class="head" style="border-bottom-style: solid; border-bottom-color:#0066CC"> <a href="../indexmain.jsp"><img alt="阜特科技" src="../images/flogo_.png"> </a> </div><div id="cutImageAvater" style="width:600px;height:400px; display:none; margin-left: 10px" ><img src="" id="target" alt="上传图片展示区" style="width:300px;height:300px;" />
<div id="preview-pane"> <div class="preview-container"> <img src="" class="jcrop-preview" alt="预览图片" id="newpicture" /> </div> </div> <br> <form action="../saveAvatar.do" method="post" > x轴:<input type="text" size="4" id="x1" name="x" readonly="true" class="inputAxiesClass"/> y轴:<input type="text" size="4" id="y1" name="y" readonly="true" class="inputAxiesClass"/> 宽度:<input type="text" size="4" id="w" name="w" readonly="true" class="inputAxiesClass"/> 高度:<input type="text" size="4" id="h" name="h" readonly="true" class="inputAxiesClass"/> <input type="submit" value="保存裁剪" class="btn btn-success"/> <input type="button" value="返回首页" onclick="goBack()" class="btn btn-primary"/> </form> </div><form action="" method="post" enctype="multipart/form-data" style="margin-top:10px"><div id="some_file_queue"></div><input type="file" name="file" id="upload_org_code" /> </form> </html>
使用jquery插件uploadfive、jcrop实现头像上传的更多相关文章
- jQuery插件AjaxFileUpload实现ajax文件上传时老是运行error方法 问题原因
今天在用jQuery插件AjaxFileUpload实现ajax文件上传时,遇到一个问题,如图: 老是运行error.无法运行succes方法,追踪ajaxfileupload.js源代码发现: wa ...
- jQuery插件AjaxFileUpload实现ajax文件上传
转自:http://www.cnblogs.com/linjiqin/p/3530848.html jQuery插件AjaxFileUpload用来实现ajax文件上传,该插件使用非常简单,接下来写个 ...
- jQuery插件之路(三)——文件上传(支持拖拽上传)
好了,这次咱一改往日的作风,就不多说废话了,哈哈.先贴上源代码地址,点击获取.然后直接进入主题啦,当然,如果你觉得我有哪里写的不对或者欠妥的地方,欢迎留言指出.在附上一些代码之前,我们还是先来了解下, ...
- jquery头像上传剪裁插件cropper的前后台demo
因为一个项目要做一个头像上传的功能,因此选择了使用jquery的头像插件cropper,cropper是一款使用简单且功能强大的图片剪裁jQuery插件,但是在使用的时候,有一个很大的坑需要注意,那就 ...
- 强大的flash头像上传插件(支持旋转、拖拽、剪裁、生成缩略图等)
今天介绍的这款flash上传头像功能非常强大,支持php,asp,jsp,asp.net 调用 头像剪裁,预览组件插件. 本组件需要安装Flash Player后才可使用,请从http://dl.pc ...
- 【Bootstrap-插件使用】Jcrop+fileinput组合实现头像上传功能
作者:Dreawer链接:https://zhuanlan.zhihu.com/p/24465742来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非商业转载请注明出处. 作者:梦游的龙猫(转 ...
- [Bootstrap-插件使用]Jcrop+fileinput组合实现头像上传功能
很久没有更新博客了,再不写点东西都烂了. 这次更新一个小内容,是两个插件的组合使用,实现头像上传功能. 业务需求: 头像上传功能,要对上传的文件进行剪切,且保证头像到服务器时必须是正方形的. 优化&l ...
- 头像上传uploadPreview插件
原文链接:https://blog.csdn.net/Alisa_L/article/details/52923953 uploadPreview 今天写头像上传,使用到uploadPreview插件 ...
- php头像上传插件
最近找到了一个比较简单实用的php头像上传插件,兼容IE8及以上等主流浏览器,分享给大家.效果如下: 1.首页效果图:默认显示默认图片. 2.点击图片(拥有裁剪框,可以拖动.缩放.裁剪头像等功能,注意 ...
随机推荐
- Selenium+python操作id为动态变化的frame(iframe)
先定位到一组frame:ele = dr.find_elements_by_tag_name('iframe')此时获得一组frame 再通过index取需要切进去的frame并取到该frame的id ...
- [python]汉诺塔问题
相传在古印度圣庙中,有一种被称为汉诺塔(Hanoi)的游戏.该游戏是在一块铜板装置上,有三根杆(编号A.B.C),在A杆自下而上.由大到小按顺序放置64个金盘(如下图).游戏的目标:把A杆上的金盘全部 ...
- Hadoop点滴-Hadoop分布式文件系统
Hadoop自带HDFS,即 Hadoop Distributed FileSystem(不是HaDoop FileSystem 的简称) 适用范围 超大文件:最新的容量达到PB级 流式数据访问:H ...
- Hadoop源代码点滴-自然常数e
数学里的 e 为什么叫做自然底数?是不是自然界里什么东西恰好是 e? https://www.zhihu.com/question/20296247
- 自定义TabLayout的Indicator
最近项目要使用类似TabLayout的控件,其实我感觉就是TabLayout只是换了一个Indicator,先说一说TabLayout这是Android Support Design的控件要使用的同学 ...
- 【SQL server基础】objectproperty()函数
SQL Server OBJECTPROPERTY使用方法 OBJECTPROPERTY 返回有关当前数据库中的模式作用域对象的信息.此函数不能用于不是模式范围的对象,例如数据定义语言(DDL)触 ...
- 通过实例快速掌握k8s(Kubernetes)核心概念
容器技术是微服务技术的核心技术之一,并随着微服务的流行而迅速成为主流.Docker是容器技术的先驱和奠基者,它出现之后迅速占领市场,几乎成了容器的代名词.但它在开始的时候并没有很好地解决容器的集群问题 ...
- HTML5 相关扩展
一.与类相关的扩展 class属性的应用极其广泛,与class的相关的操作也越来越简化,HTML5增加了 getElementsByClassName来查找元素,通过也增加了classList属性,方 ...
- Java面向对象程序设计第6章1-12
1.this和super各有几种用法? this this 用法有三种,主要应用场合: 表示当前对象引用,常用于形参或局部变量与类的成员变 量同名的情形,使用this.成员名表示当前对象的成员 表示当 ...
- 【柠檬班】jmeter 不写代码,秒秒钟提取动态列表最后一个值
在用jmeter做接口测试时,我们经常会遇到,一个接口返回一个json串,在这个json串中,某个节点的值是一个列表,而且这个列表的长度是动态变化的.如: 获取用户列表,用户信息是个列表,类似的接 ...