jquery-file-upload 文件上傳帶進(jìn)度條效果
jQuery File Upload 是一個(gè)Jquery圖片上傳組件,支持多文件上傳、取消、刪除,上傳前縮略圖預(yù)覽、列表顯示圖片大小,支持上傳進(jìn)度條顯示;支持各種動(dòng)態(tài)語(yǔ)言開(kāi)發(fā)的服務(wù)器端。
效果圖如下所示:
html 部分
<div style="line-height:34px;margin-top:20px;"> <label style="float: left;font-size:14px">上傳文件:</label> <span class="btn btn-success fileinput-button fn-left"> <i class="glyphicon glyphicon-plus"></i> <span>瀏覽...</span> <input type="file" name="file" id="file_upload"> </span> <div style="float: left;margin-left: 20px;font-weight: bold" id="uploadText"></div> </div> <div class="fn-clear"></div> <div id="progress"> <div class="bar" style="width: 0%;"></div> </div>
css 部分
<link rel="stylesheet" href="/admin/assets/plugins/jquery-file-upload/css/jquery.fileupload-ui.css" rel="external nofollow" > <link rel="stylesheet" href="/admin/assets/plugins/jquery-file-upload/css/jquery.fileupload.css" rel="external nofollow" > /*文件上傳控件*/ .bar { background-image: -webkit-linear-gradient(top,#5cb85c 0,#449d44 100%); background-image: -o-linear-gradient(top,#5cb85c 0,#449d44 100%); background-image: -webkit-gradient(linear,left top,left bottom,from(#5cb85c),to(#449d44)); background-image: linear-gradient(to bottom,#5cb85c 0,#449d44 100%); filter: progid:DXImageTransform.Microsoft.gradient(startColorstr='#ff5cb85c', endColorstr='#ff449d44', GradientType=0); background-repeat: repeat-x; height: 20px; font-size: 12px; line-height: 20px; color: #fff; text-align: center; background-color: #428bca; -webkit-box-shadow: inset 0 -1px 0 rgba(0,0,0,.15); box-shadow: inset 0 -1px 0 rgba(0,0,0,.15); -webkit-transition: width .6s ease; -o-transition: width .6s ease; transition: width .6s ease; } #progress { background-image: -webkit-linear-gradient(top,#ebebeb 0,#f5f5f5 100%); background-image: -o-linear-gradient(top,#ebebeb 0,#f5f5f5 100%); background-image: -webkit-gradient(linear,left top,left bottom,from(#ebebeb),to(#f5f5f5)); background-image: linear-gradient(to bottom,#ebebeb 0,#f5f5f5 100%); filter: progid:DXImageTransform.Microsoft.gradient(startColorstr='#ffebebeb', endColorstr='#fff5f5f5', GradientType=0); background-repeat: repeat-x; height: 20px; width: 0%; margin-bottom: 20px; overflow: hidden; background-color: #f5f5f5; border-radius: 4px; -webkit-box-shadow: inset 0 1px 2px rgba(0,0,0,.1); box-shadow: inset 0 1px 2px rgba(0,0,0,.1); margin-top: 20px; } .glyphicon { position: relative; top: 1px; display: inline-block; font-family: 'Glyphicons Halflings'; font-style: normal; font-weight: 400; line-height: 1; -webkit-font-smoothing: antialiased; -moz-osx-font-smoothing: grayscale; } .glyphicon-plus:before { content: "\2b"; } .btn-success { color: #fff; background-color: #5cb85c; border-color: #4cae4c; } .btn { display: inline-block; padding: 6px 12px; margin-bottom: 0; font-size: 14px; font-weight: 400; line-height: 1.42857143; text-align: center; white-space: nowrap; vertical-align: middle; cursor: pointer; -webkit-user-select: none; -moz-user-select: none; -ms-user-select: none; user-select: none; background-image: none; border: 1px solid transparent; border-radius: 4px; }
js 部分
<script src="/admin/assets/plugins/jquery-file-upload/js/vendor/jquery.ui.widget.js"></script> <script src="/admin/assets/plugins/jquery-file-upload/js/jquery.fileupload.js"></script> <script type="text/javascript" src="${pageContext.request.contextPath}/admin/assets/plugins/jquery-1.10.2.min.js"></script>
<span style="font-family:monospace;font-size:14px;"> <span id="_xhe_cursor"></span>$('#file_upload').fileupload({ dataType: 'json', url:'${pageContext.request.contextPath}/excel/upload', autoUpload:false, add: function (e, data) { $('#progress').css( 'width','0%' ); $('#progress .bar').css( 'width', '0%' ); $("#uploadText").empty(); var fileType = data.files[0].name.split('.').pop(); // console.log(data); var acceptFileTypes = /xls|xlsx$/i; var size = data.files[0].size; size = (size/1024).toFixed(2);//文件大小單位kb var maxFileSize = 5*1024;//最大允許文件大小單位kb if (!acceptFileTypes.test(fileType)) { new Message({message:"不支持的文件類型,僅支持EXCEL文件"}); return ; } if(size>maxFileSize){ new Message({message:"文件大?。?+size+"KB,超過(guò)最大限制:"+maxFileSize+"KB"}); return ; } data.context = $("<button class=' ui-button ui-button-lwhite'/>").text("上傳") .appendTo("#uploadText") .click(function () { data.context = $("<p/>").text("正在上傳...").replaceAll($(this)); data.submit(); }); }, progressall: function (e, data) { var progress = parseInt(data.loaded / data.total * 100, 10); $('#progress').css( 'width','100%' ); $('#progress .bar').css( 'width',progress + '%' ); }, fail:function (e, data) { new Message({message:"上傳失敗"}); }, done: function (e, data) { console.log(data.files[0]); var fileName = data.files[0].name; var size = data.files[0].size; var obj = data.result; if(obj.success == true){ $("#filePath").val(obj.result.fileId+"&"+obj.result.opLogId); data.context.text("文件上傳已完成!文件名:"+fileName+" 文件大?。?+size+"kb"); }else{ alert(obj.errorMsg); } } });</span>
XHR響應(yīng)為Json時(shí)IE的下載BUG
這里需要特別注意的是,由于jQuery File Upload都是采用XHR在傳遞數(shù)據(jù),服務(wù)器端返回的通常是JSON格式的響應(yīng),但是IE會(huì)將這些JSON響應(yīng)誤認(rèn)為是文件傳輸,然后直接彈出下載框詢問(wèn)是否需要下載。
解決這個(gè)問(wèn)題的方法是必須將相應(yīng)的Http Head從
Content-Type: application/json
更改為
Content-Type: text/
總結(jié)
以上所述是小編給大家介紹的jquery-file-upload 文件上傳帶進(jìn)度條效果,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
- 基于jquery步驟進(jìn)度條源碼分享
- jquery+ajax實(shí)現(xiàn)上傳圖片并顯示上傳進(jìn)度功能【附php后臺(tái)接收】
- jQuery實(shí)現(xiàn)的手動(dòng)拖動(dòng)控制進(jìn)度條效果示例【測(cè)試可用】
- jQuery NProgress.js加載進(jìn)度插件的簡(jiǎn)單使用方法
- jQuery實(shí)現(xiàn)可拖動(dòng)進(jìn)度條實(shí)例代碼
- jquery網(wǎng)頁(yè)加載進(jìn)度條的實(shí)現(xiàn)
- jquery 步驟進(jìn)度軸插件的實(shí)現(xiàn)代碼
相關(guān)文章
EasyUI在表單提交之前進(jìn)行驗(yàn)證的實(shí)例代碼
這篇文章主要介紹了EasyUI在表單提交之前進(jìn)行驗(yàn)證的實(shí)例代碼的相關(guān)資料,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2016-06-06jquery選擇器和屬性對(duì)象的操作實(shí)例分析
這篇文章主要介紹了jquery選擇器和屬性對(duì)象的操作,結(jié)合實(shí)例形式分析了jquery選擇器與頁(yè)面元素屬性相關(guān)操作技巧,需要的朋友可以參考下2020-01-01基于jQuery實(shí)現(xiàn)定位導(dǎo)航位置效果
這篇文章主要為大家詳細(xì)介紹了基于jQuery實(shí)現(xiàn)定位導(dǎo)航位置效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-11-11jQuery選擇id屬性帶有點(diǎn)符號(hào)元素的方法
這篇文章主要介紹了jQuery選擇id屬性帶有點(diǎn)符號(hào)元素的方法,涉及jQuery選擇器的使用技巧,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2015-03-03基于jquery的橫向滾動(dòng)條(滑動(dòng)條)
ASP.Net的GridView本身不帶滾動(dòng)條,可通過(guò)Panel實(shí)現(xiàn)。但是Windows自帶的橫向滾動(dòng)條只支持顯示在下方,為了使用方便,需要在上下方都顯示橫向滾動(dòng)條。2011-02-02使用jquery datatable和bootsrap創(chuàng)建表格實(shí)例代碼
這篇文章主要介紹了使用jquery-datatable和bootsrap創(chuàng)建表格的實(shí)例代碼,需要的朋友可以參考下2017-03-03JQery jstree 大數(shù)據(jù)量問(wèn)題解決方法
Jquery 結(jié)合jstree 動(dòng)態(tài)生成一棵樹(shù),如果某一節(jié)點(diǎn)下目錄超過(guò)500個(gè),IE 會(huì)提示是否允許JS腳本運(yùn)行,并且目錄加載不全,大約只加載了300左右。2010-03-03