自己造輪子之上傳控制項

來源:互聯網
上載者:User

標籤:如何   retina   move   進度   參數   bsp   sage   extension   ict   

前言:

      本次項目裡面需要使用上傳外掛程式,網上找了很多三方外掛程式都被產品PASS

      後面找到了webiploader,然後對他進行了模組化的封裝。

1.js的模組化管理

     

     本文中使用了requier.js來管理。 至於如何使用大家可以百度一下!

     主要是基於webiploader,然後進行一些樣式處理。

     先上一個:

     

    接受了兩個參數和一個配置參數,

    配置參數如不傳,會有預設值。

   

  

   

    

      附上代碼

  1 define([‘jquery‘, ‘webuploader‘], function ($, WebUploader) {  2     "use strict";  3     /*  4      上傳控制項 by shumin 2016-11-25  5      ist  圖片顯示地區ID  6      pick 上傳按鈕地區ID  7      opts 控制項參數  8     */  9      10      11     function MyWebUploader(list,pick, opts) { 12         this.$list = $(list); 13         this.$pick = $(pick); 14         this.$ = $; 15         this.opts = $.extend({}, MyWebUploader.DEFAULTS, opts); 16  17         this.init(); 18  19          20  21  22     } 23     MyWebUploader.DEFAULTS = { 24         type:‘webfile‘,   //上傳類型  (不同類型 儲存路徑不同) 25         fileNumLimit: 1,  //上傳數量 26         thumbnailWidth: 90,  //縮圖寬 27         thumbnailHeight:90   //縮圖高 28     } 29     MyWebUploader.prototype.init = function () { 30         var self = this; 31  32           33             // 最佳化retina, 在retina下這個值是2 34         var ratio = window.devicePixelRatio || 1, 35             // 縮圖大小 36             thumbnailWidth = self.opts.thumbnailWidth * ratio, 37             thumbnailHeight = self.opts.thumbnailHeight * ratio, 38  39             // Web Uploader執行個體 40             uploader; 41         uploader = WebUploader.create({ 42             // 選完檔案後,是否自動上傳。 43             auto: false, 44  45             disableGlobalDnd: true, 46             // swf檔案路徑 47             swf: ‘/img/js/plugin/webuploader/Uploader.swf‘, 48  49             // 檔案接收服務端。 50             server: ‘/API/Upload?type=‘ + self.opts.type, 51  52             // 選擇檔案的按鈕。可選。 53             // 內部根據當前運行是建立,可能是input元素,也可能是flash. 54             pick:self.$pick, 55             fileNumLimit: self.opts.fileNumLimit, 56             //只允許選擇圖片 57             accept: { 58                 title: ‘Images‘, 59                 extensions: ‘gif,jpg,jpeg,bmp,png‘, 60                 mimeTypes: ‘image/*‘ 61             } 62  63         }); 64  65         // 當有檔案添加進來的時候 66         uploader.on(‘fileQueued‘, function (file) { 67             var $li = $( 68                     ‘<div id="‘ + file.id + ‘" class="cp_img">‘ + 69                         ‘<img>‘ + 70                     ‘<div class="cp_img_jian"></div></div>‘ 71                     ), 72                 $img = $li.find(‘img‘); 73  74  75             // $list為容器jQuery執行個體 76             self.$list.append($li); 77  78             // 建立縮圖 79             // 如果為非圖片檔案,可以不用調用此方法。 80             // thumbnailWidth x thumbnailHeight 為 100 x 100 81             uploader.makeThumb(file, function (error, src) { 82                 if (error) { 83                     $img.replaceWith(‘<span>不能預覽</span>‘); 84                     return; 85                 } 86  87                 $img.attr(‘src‘, src); 88             }, thumbnailWidth, thumbnailHeight); 89             uploader.upload(); 90         }); 91  92         // 檔案上傳過程中建立進度條即時顯示。 93         uploader.on(‘uploadProgress‘, function (file, percentage) { 94             var $li = $(‘#‘ + file.id), 95                 $percent = $li.find(‘.progress span‘); 96  97             // 避免重複建立 98             if (!$percent.length) { 99                 $percent = $(‘<p class="progress"><span></span></p>‘)100                         .appendTo($li)101                         .find(‘span‘);102             }103 104             $percent.css(‘width‘, percentage * 100 + ‘%‘);105         });106 107         // 檔案上傳成功,給item添加成功class, 用樣式標記上傳成功。108         uploader.on(‘uploadSuccess‘, function (file, response) {109             if (response.Status == 1) {110 111                 // $(‘#‘ + file.id).children(‘img‘).attr(‘src‘, response.Message);112             }113             $(‘#‘ + file.id).addClass(‘upload-state-done‘);114         });115 116         // 檔案上傳失敗,顯示上傳出錯。117         uploader.on(‘uploadError‘, function (file) {118             var $li = $(‘#‘ + file.id),119                 $error = $li.find(‘div.error‘);120 121             // 避免重複建立122             if (!$error.length) {123                 $error = $(‘<div class="error"></div>‘).appendTo($li);124             }125 126             $error.text(‘上傳失敗‘);127         });128 129         // 完成上傳完了,成功或者失敗,先刪除進度條。130         uploader.on(‘uploadComplete‘, function (file) {131             $(‘#‘ + file.id).find(‘.progress‘).remove();132         });133 134         //所有檔案上傳完畢135         uploader.on("uploadFinished", function () {136             //提交表單137 138         });139 140         //開始上傳141         $("#ctlBtn").click(function () {142             uploader.upload();143 144         });145 146         //顯示刪除按鈕147 148         $(document).on(‘mouseover‘, ‘.cp_img‘, function () {149             $(this).children(".cp_img_jian").css(‘display‘, ‘block‘);150         })151         //隱藏刪除按鈕152         $(document).on(‘mouseout‘, ‘.cp_img‘, function () {153             $(this).children(".cp_img_jian").css(‘display‘, ‘none‘);154         })155 156 157         //執行刪除方法158         self.$list.on("click", ".cp_img_jian", function () {159             var Id = $(this).parent().attr("id");160             uploader.removeFile(uploader.getFile(Id, true));161             $(this).parent().remove();162         });163 164     }165 166 167     return{168         MyWebUploader: MyWebUploader169     } 170 })

 後面調用就很方便了,不需要每次使用都寫一大坨配置項

 

 

     貼上頁面代碼

    

 

 

 

 


 

自己造輪子之上傳控制項

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.