jQuery自定義圖片上傳插件實(shí)例代碼
摘要
1.jquery自定義插件方法
2.表單file樣式調(diào)整
3.利用formData,ajax上傳圖片
4.js,css彈出層
5.springmvc上傳圖片
效果
調(diào)用方式
$("#picUrl").imgUpload({}),
在代碼內(nèi)部為調(diào)用對(duì)象綁定了click事件,點(diǎn)擊彈出上傳界面。
$(function(){ $("#picUrl").imgUpload({url:'<%=basePath%>'+'file/upload.do'}) $("#picUrl").imgUpload("resize");/**彈出層水平垂直居中**/ })
jquery自定義插件要點(diǎn)
1.定義作用域
2.$.fn.***為每個(gè)jquery對(duì)象擴(kuò)展方法
3.設(shè)置默認(rèn)值
4.return this.each,支持鏈?zhǔn)秸{(diào)用
/**部分代碼**/ (function($){ $.fn.imgUpload=function(options,param){ if(typeof options =="string"){ return $.fn.imgUpload.methods[options](this,param); } /**this為jquery對(duì)象**/ options = options || {}; return this.each(function() { /**this 為 dom 對(duì)象**/ 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樣式調(diào)整
.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; }
后臺(tái)部分代碼
@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("-",""); //獲得文件類(lèi)型(可以判斷如果不是圖片,禁止上傳) 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自定義圖片上傳插件實(shí)例代碼,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)歡迎給我留言,小編會(huì)及時(shí)回復(fù)大家的,在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
- 利用jQuery插件imgAreaSelect實(shí)現(xiàn)圖片上傳裁剪(放大縮小)
- jQuery插件uploadify實(shí)現(xiàn)ajax效果的圖片上傳
- jQuery實(shí)現(xiàn)圖片上傳和裁剪插件Croppie
- jQuery插件jcrop+Fileapi完美實(shí)現(xiàn)圖片上傳+裁剪+預(yù)覽的代碼分享
- Jquery uploadify圖片上傳插件無(wú)法上傳的解決方法
- jquery上傳插件fineuploader上傳文件使用方法(jquery圖片上傳插件)
- 圖片上傳插件jquery.uploadify詳解
- jquery 圖片上傳按比例預(yù)覽插件集合
相關(guān)文章
jquery實(shí)現(xiàn)Ctrl+Enter提交表單的方法
這篇文章主要介紹了jquery實(shí)現(xiàn)Ctrl+Enter提交表單的方法,涉及jquery針對(duì)鍵盤(pán)按鍵的響應(yīng)與表單操作相關(guān)技巧,非常簡(jiǎn)單實(shí)用,需要的朋友可以參考下2015-07-07jQuery實(shí)現(xiàn)購(gòu)物車(chē)多物品數(shù)量的加減+總價(jià)計(jì)算
這篇文章主要介紹了jQuery實(shí)現(xiàn)購(gòu)物車(chē)多物品數(shù)量的加減+總價(jià)計(jì)算,需要的朋友可以參考下2014-06-06修復(fù)jQuery tablesorter無(wú)法正確排序的bug(加千分位數(shù)字后)
這篇文章主要介紹了如何修復(fù)jQuery tablesorter無(wú)法正確排序的bug(加千分位數(shù)字后)的相關(guān)資料,需要的朋友可以參考下2016-03-03jQuery+C#實(shí)現(xiàn)參數(shù)RSA加密傳輸功能【附j(luò)sencrypt.js下載】
這篇文章主要介紹了jQuery+C#實(shí)現(xiàn)參數(shù)RSA加密傳輸功能,結(jié)合具體實(shí)例形式分析了js使用jsencrypt.js插件前端字符數(shù)據(jù)處理傳輸及C#后臺(tái)數(shù)據(jù)轉(zhuǎn)換與RSA加密相關(guān)操作技巧,并附帶jsencrypt.js供讀者下載參考使用,需要的朋友可以參考下2017-06-06JQuery Tab選項(xiàng)卡效果代碼改進(jìn)版
JQuery制作的選項(xiàng)卡改進(jìn)版,已經(jīng)有人將這個(gè)效果改進(jìn)下封裝成一個(gè)完整的方法。2010-04-04jquery和雅虎的yql服務(wù)實(shí)現(xiàn)天氣預(yù)報(bào)服務(wù)示例
本文介紹一個(gè)利用Jquery和雅虎的YQL服務(wù)實(shí)現(xiàn)天氣預(yù)報(bào)功能,需要的朋友可以參考下2014-02-02jquery地址欄鏈接與a標(biāo)簽鏈接匹配之特效代碼總結(jié)
jquery如何獲取地址欄參數(shù),改變地址欄樣式,接下來(lái),通過(guò)本篇文章給大家分享jquery地址欄鏈接與a標(biāo)簽鏈接匹配之特效代碼總結(jié),需要的朋友可以參考下2015-08-08