摘要
1.jquery自定义插件方法
2.表单file样式调整
3.利用formData,ajax上传图片
4.js,css弹出层
5.springmvc上传图片
效果
调用方式
$("#picUrl").imgUpload({}),
在代码内部为调用对象绑定了click事件,点击弹出上传界面。
$(function(){ $("#picUrl").imgUpload({url:'<%=basePath%>'+'file/upload.do'}) $("#picUrl").imgUpload("resize");/**弹出层水平垂直居中**/ })
jquery自定义插件要点
1.定义作用域
2.$.fn.***为每个jquery对象扩展方法
3.设置默认值
4.return this.each,支持链式调用
/**部分代码**/ (function($){ $.fn.imgUpload=function(options,param){ if(typeof options =="string"){ return $.fn.imgUpload.methods[options](this,param); } /**this为jquery对象**/ options = options || {}; return this.each(function() { /**this 为 dom 对象**/ var state=$.data(this,"imgUploadData"); var opts; if(state){ opts = $.extend(state.options, options); state.options = opts; }else{ opts = $.extend({},$.fn.imgUpload.defaults,options); $.data(this,"imgUploadData",{options:opts}); } init(this); }); }; $.fn.imgUpload.methods={ resize:function(jq){ $(".main-layer").css({ left:($(window).width()-$(".main-layer").outerWidth())/2, top:($(window).height()-$(".main-layer").outerHeight())/2 }); } } $.fn.imgUpload.defaults={ width:100, height:200, url:'#' } })(jQuery);
利用formData,ajax上传文件
/**html5 formData**/ function upload(jq){ var formData=new FormData(); var opts = $.data(jq,"imgUploadData").options; formData.append('file',$("#imgFile")[0].files[0]); $.ajax({ url: opts.url, type: 'POST', cache: false, data: formData, processData: false, contentType: false, success:function(url){ console.info(url); }, error:function(url){ console.info(url); } }) }
表单file样式调整
.main-layer .a-upload { padding: 4px 10px; height: 20px; line-height: 20px; position: relative; cursor: pointer; color: #888; background: #fafafa; border: 1px solid #ddd; border-radius: 4px; overflow: hidden; display: inline-block; *display: inline; *zoom: 1 ; width:90%; text-align: center; } .a-upload input { position: absolute; font-size: 100px; right:0; top: 0; opacity: 0; filter: alpha(opacity=0); cursor: pointer }
js,css弹出层样式
/***遮罩层样式**/ .wrap-overlayer{ position: fixed; left: 0; top:0; width: 100%; height: 100%; background-color: rgba(0,0,0,0.3); z-index:10; display:none; } /**上传组件样式**/ .main-layer{ position:absolute; left:50%;top:50%; background-color: #fff; width:350px; height: 150px; }
后台部分代码
@RequestMapping(value="/upload.do",method=RequestMethod.POST) private void fildUpload(@RequestParam(value="file",required=false) MultipartFile file, HttpServletRequest request,HttpServletResponse resp)throws Exception{ //获得物理路径webapp所在路径 String pathRoot = request.getSession().getServletContext().getRealPath(""); String path=""; if(!file.isEmpty()){ //生成uuid作为文件名称 String uuid = UUID.randomUUID().toString().replaceAll("-",""); //获得文件类型(可以判断如果不是图片,禁止上传) String contentType=file.getContentType(); //获得文件后缀名称 String imageName=contentType.substring(contentType.indexOf("/")+1); path="/images/"+uuid+"."+imageName; file.transferTo(new File(pathRoot+path)); } request.setAttribute("imagesPath", path); }
以上所述是小编给大家介绍的jQuery自定义图片上传插件实例代码,希望对大家有所帮助,如果大家有任何疑问欢迎给我留言,小编会及时回复大家的,在此也非常感谢大家对网站的支持!
华山资源网 Design By www.eoogi.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
华山资源网 Design By www.eoogi.com
暂无评论...
更新日志
2024年09月25日
2024年09月25日
- 杨千嬅.2002-万紫千红演唱会2CD(2024环球红馆40复刻系列)【环球】【WAV+CUE】
- 郭采洁.2015-Begin.Again爱造飞鸡【华纳】【FLAC分轨】
- 许志安.2011-ON.AND.ON【东亚】【WAV+CUE】
- 潘秀琼.1994-木兰从军【名将】【WAV+CUE】
- 纯音入心系列纯音乐《古筝系列-当流行乐遇到古筝》1CD[MP3][1.9GB]
- 纯音入心系列纯音乐《精选古筝名曲100首》1CD[MP3][388.7MB]
- 纯音入心系列纯音乐《天籁古筝》1CD[MP3][331MB]
- 男女对唱典藏天碟《发烧对唱·那个季节里的歌DSD》10CD[WAV]
- 群星2010-歌林精选辑[歌林][WAV+CUE]
- TrioZimbalist-PianoTriosofWeinberg,Auerbach,Dvorak(2024)[24-44,1]wav
- 群星.1992-滚石第一流台湾歌·12王牌大车拼【滚石】【WAV+CUE】
- 林子祥.1984-创作歌集【华纳】【WAV+CUE】
- 周汤豪.2010-周汤豪【华纳】【FLAC分轨】
- Mozart-TheStringQuintets-AmadeusQuartet,CecilAronowitz(2017)[24-44,1][WAV+CUE]
- JamesWilliamsDennisIrwin-Focus(2024,Red)[24-48]FLAC