使用plupload自訂參數實現多檔案上傳_javascript技巧

來源:互聯網
上載者:User

1、在開發中可能需要使用者附件上傳的功能,實現批量上傳功能其實就將多個上傳任務放到一個集合中,分別上傳。

2,使用plupload js外掛程式可以很輕鬆的實現帶參數的多檔案上傳

3、具體js實現方式

<script type="text/javascript">var map={};//檔案id為key,檔案類型為value的集合// Custom example logicvar uploader = new plupload.Uploader({runtimes : 'html5,flash,silverlight,html4',browse_button : 'pickfiles', // you can pass in id...container : document.getElementById('container'), // ... or DOM Element itselfurl : '../action/photoUpload.jsp',flash_swf_url : '../../js/Moxie.swf',silverlight_xap_url : '../../js/Moxie.xap',filters : {max_file_size : '5mb',mime_types : [ {title : "圖片類型",extensions : "jpg,gif,png"} ]},init : {PostInit : function() {document.getElementById('uploadfiles').onclick = function() {uploader.start();return false;};},BeforeUpload:function(up,file){//重點在這裡,上傳的時候自訂參數資訊uploader.setOption("multipart_params",{"apptype":${param.type},"id":${param.id},"fileType":map[file.id]});},FileUploaded:function(up,file){$("#"+file.id+"btn").parent().html("上傳完畢");$("#"+file.id+"btn").remove();},FilesAdded : function(up, files) {if($("#fileType").val()==1){alert("請選擇附件類型!");//清空剛才選擇的檔案plupload.each(files,function(file) {uploader.removeFile(file.id);});return false;}plupload.each(files,function(file) {map[file.id]=$("#fileType").val();$("#uploadTable").append("<tr id='"+file.id+"tr'><td>"+$("#fileType").val()+"</td><td>"+file.name+"</td><td><button type='button' id='"+file.id+"btn' onclick=del('"+file.id+"')>刪除</button> </td><td id='"+file.id+"_progress'></td></tr>");});},UploadProgress : function(up, file) {$("#"+file.id+"_progress").html('<span>'+ file.percent + "%</span>");},Error : function(up, err) {alert("此檔案上傳失敗!");}}});uploader.init();//根據檔案id刪除此檔案,並且在table中刪除此檔案的資訊function del(id){uploader.removeFile(id);$("#"+id+"tr").remove();map[id]="";}</script>

其他參數在其官網上:http://www.plupload.com/

宅男程式猿,文采不行,如果看著累直接複製js,注意頁面上需要有:

<div id="filelist"></div><div id="container"><center>提示:必須上傳 "商戶營業執照","稅務登記證明","企業法人身份證","考察照片","商戶資訊調查表","商戶協議" 才能提交審批!</center><label>附件類型:</label> <select name="fileType" id="fileType" ><option value="1">-------請選擇-----</option><option value="商戶營業執照">商戶營業執照</option><option value="稅務登記證明">稅務登記證明</option><option value="商戶資訊變更表">商戶資訊變更表</option><option value="其他">其他</option></select><button class="button" id="pickfiles">選擇檔案</button>  <button class="button" id="uploadfiles">批量上傳</button>  <button class="button" onclick="window.location.reload()">重新整理</button>  <button class="button" onclick="window.close()">關閉視窗</button><table style="width: 600px" id="uploadTable" border="1"><tr><td>檔案類型</td><td>檔案名稱</td><td>操作</td><td>上傳進度</td></tr></table></div>

以上所述是小編給大家介紹的使用plupload自訂參數實現多檔案上傳,希望對大家有所協助,如果大家有任何疑問請給我留言,小編會及時回複大家的。在此也非常感謝大家對雲棲社區網站的支援!

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.