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实现头像上传的更多相关文章

  1. jQuery插件AjaxFileUpload实现ajax文件上传时老是运行error方法 问题原因

    今天在用jQuery插件AjaxFileUpload实现ajax文件上传时,遇到一个问题,如图: 老是运行error.无法运行succes方法,追踪ajaxfileupload.js源代码发现: wa ...

  2. jQuery插件AjaxFileUpload实现ajax文件上传

    转自:http://www.cnblogs.com/linjiqin/p/3530848.html jQuery插件AjaxFileUpload用来实现ajax文件上传,该插件使用非常简单,接下来写个 ...

  3. jQuery插件之路(三)——文件上传(支持拖拽上传)

    好了,这次咱一改往日的作风,就不多说废话了,哈哈.先贴上源代码地址,点击获取.然后直接进入主题啦,当然,如果你觉得我有哪里写的不对或者欠妥的地方,欢迎留言指出.在附上一些代码之前,我们还是先来了解下, ...

  4. jquery头像上传剪裁插件cropper的前后台demo

    因为一个项目要做一个头像上传的功能,因此选择了使用jquery的头像插件cropper,cropper是一款使用简单且功能强大的图片剪裁jQuery插件,但是在使用的时候,有一个很大的坑需要注意,那就 ...

  5. 强大的flash头像上传插件(支持旋转、拖拽、剪裁、生成缩略图等)

    今天介绍的这款flash上传头像功能非常强大,支持php,asp,jsp,asp.net 调用 头像剪裁,预览组件插件. 本组件需要安装Flash Player后才可使用,请从http://dl.pc ...

  6. 【Bootstrap-插件使用】Jcrop+fileinput组合实现头像上传功能

    作者:Dreawer链接:https://zhuanlan.zhihu.com/p/24465742来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非商业转载请注明出处. 作者:梦游的龙猫(转 ...

  7. [Bootstrap-插件使用]Jcrop+fileinput组合实现头像上传功能

    很久没有更新博客了,再不写点东西都烂了. 这次更新一个小内容,是两个插件的组合使用,实现头像上传功能. 业务需求: 头像上传功能,要对上传的文件进行剪切,且保证头像到服务器时必须是正方形的. 优化&l ...

  8. 头像上传uploadPreview插件

    原文链接:https://blog.csdn.net/Alisa_L/article/details/52923953 uploadPreview 今天写头像上传,使用到uploadPreview插件 ...

  9. php头像上传插件

    最近找到了一个比较简单实用的php头像上传插件,兼容IE8及以上等主流浏览器,分享给大家.效果如下: 1.首页效果图:默认显示默认图片. 2.点击图片(拥有裁剪框,可以拖动.缩放.裁剪头像等功能,注意 ...

随机推荐

  1. mysql--命令行创建数据库

    MySQL 1. 连接到本机上的MYSQL. 首先打开DOS窗口,然后进入目录mysql\bin,再键入命令mysql -u root  -p,回车后提示你输密码.注意用户名前可以有空格也可以没有空格 ...

  2. 一个js破解教程

    很好的一篇文章,先存着以后用到. 为了防止官网更新修复,存一下版本 https://pan.lanzou.com/b220073/ 密码:这是秘密 这篇文章以 JavaScript 为例讲解了破解的一 ...

  3. java获取配置文件中的key=value值

    1.献上工具类 package com.test.util; import java.io.FileInputStream; import java.io.FileNotFoundException; ...

  4. JS 时间格式 相互转化

    1. 时间字符串格式 var dateString1 = '2016-06-15 10:22:00'; var dateString2 = '2016/06/15 10:22:00'; var dat ...

  5. 冒泡排序--JavaScript描述

    相信凡是编程入门的都接触过冒泡排序算法,排序算法在编程中经常用到. 1. code /** * 冒泡排序 * 1.比较的轮数等于总数 - 1 * 2.比较次数等于要比较的个数 - 1 * --比较从第 ...

  6. redis常用笔记(第一版)

    1.SINTER 说明:多key之间取交集数据 key1 = {a,b,c,d} key2 = {c} key3 = {a,c,e} SINTER key1 key2 key3 = {c} 2.sad ...

  7. Python多任务之线程

    多任务介绍 我们先来看一下没有多任务的程序 import time def sing(): for i in range(5): print("我喜欢唱") time.sleep( ...

  8. Java 学习笔记之 Suspend和Resume

    Suspend和Resume: Suspend和Resume使用方法: 以下例子证明了线程确实被暂停了,而且还可以恢复成运行状态. public class SuspendResumeThread e ...

  9. javascript基础修炼(13)——记一道有趣的JS脑洞练习题

    目录 一. 题目 二. 解法风暴 示例代码托管在:http://www.github.com/dashnowords/blogs 博客园地址:<大史住在大前端>原创博文目录 华为云社区地址 ...

  10. Unicode 和 UTF-8 之间的关系

    一.ASCII 码 我们知道,计算机内部,所有信息最终都是一个二进制值.每一个二进制位(bit)有0和1两种状态,因此八个二进制位就可以组合出256种状态,这被称为一个字节(byte).也就是说,一个 ...