使用layui怎么實(shí)現(xiàn)一個(gè)多圖片上傳功能-創(chuàng)新互聯(lián)

這篇文章給大家介紹使用layui怎么實(shí)現(xiàn)一個(gè)多圖片上傳功能,內(nèi)容非常詳細(xì),感興趣的小伙伴們可以參考借鑒,希望對(duì)大家能有所幫助。

10年積累的做網(wǎng)站、成都做網(wǎng)站經(jīng)驗(yàn),可以快速應(yīng)對(duì)客戶對(duì)網(wǎng)站的新想法和需求。提供各種問題對(duì)應(yīng)的解決方案。讓選擇我們的客戶得到更好、更有力的網(wǎng)絡(luò)服務(wù)。我雖然不認(rèn)識(shí)你,你也不認(rèn)識(shí)我。但先網(wǎng)站制作后付款的網(wǎng)站建設(shè)流程,更有蓬萊免費(fèi)網(wǎng)站建設(shè)讓你可以放心的選擇與我們合作。
//給圖片添加刪除
    function mouseChange() {
        $(document).on("mouseenter mouseleave", ".file-iteme", function (event) {
          if (event.type === "mouseenter") {
            //鼠標(biāo)懸浮
            $(this).children(".info").fadeIn("fast");
            $(this).children(".handle").fadeIn("fast");
          } else if (event.type === "mouseleave") {
            //鼠標(biāo)離開
            $(this).children(".info").hide();
            $(this).children(".handle").hide();
          }
        });
 
    }
    mouseChange();
 
  //json的length 
  function getJsonLength(jsonData){
      var jsonLength = 0;
      for(var item in jsonData){
        jsonLength++;
      }
      return jsonLength;
    }
 
    //多圖片上傳
    var ImgList = $('#uploader-list'),uploadListIns =upload.render({
      elem: '#chooseImg',
      url: $("#projectUrl").val()+ '/img/imgUpload.do',
      accept: 'images',
      acceptMime: 'image/jpg,image/png,image/jpeg',
      exts: 'jpg|png|jpeg',
      size: 1024,
      multiple: true,
      auto: true,
      choose: function(obj){
        var files = obj.pushFile(); //將每次選擇的文件追加到文件隊(duì)列
        var len = getJsonLength(files);
        //讀取本地文件
        obj.preview(function (index, file, result) {
          if (parseInt(len)+parseInt(coachPicsArray.length)-count > 6){
            layer.msg("門店圖片不能超過6張");
            //遍歷
            $.each(files,function(_key){
              var key = _key;
              var value = files[_key];
              if(_key == index)
              { //刪除
                delete files[_key];
              }
            });
          }else {
            var reader = new FileReader();
            reader.onload = function (e) {
              var image = new Image();
              image.onload = function () {
                var realWidth = image.width;
                var realHeight = image.height;
                var tr = $(['<div id="upload-' + index + '" class="file-iteme">' +
                '<div class="removeIcon handle"> <i class="layui-icon" >&#xe640;</i></div>' +
                '<img  "showBig(this)" src=' + result + ' id="img-' + index + '">' +
                '</div>'].join(''));
 
                //刪除
                tr.find('.handle').on('click', function () {
                  $(this).parent().remove();
                  delete files[index]; //刪除對(duì)應(yīng)的文件
                  var value = $("#clubCoachPics").val().split(",");
                  var arr = [];
                  for( var i in value){
                    if (value[i] != $(this).next().data('value')){
                      arr.push(value[i]);
                    }
                  }
                  $("#clubCoachPics").val(arr.join(",")) ;
 
                });
                ImgList.append(tr);
 
              };
              image.src = result;
            };
            //正式讀取文件
            reader.readAsDataURL(file);
          }
        });
      },
      before: function (obj) {
        layer.msg('圖片上傳中...', {
          icon: 16,
          shade: 0.01,
          time: 3000
        })
      },
      done: function (res, index, upload) {
        $("#img-"+ index + "").attr("data-value",res.imgUrl);
        if(res.code == 0){ //上傳成功
          var imgUrl = $("#clubCoachPics").val();
          if(imgUrl==null||imgUrl==""){
            $("#clubCoachPics").val(res.imgUrl);
          }else{
            $("#clubCoachPics").val(imgUrl+","+res.imgUrl);
          }
          delete files[index]; //刪除文件隊(duì)列已經(jīng)上傳成功的文件
          return;
        };
 
      }
    });

關(guān)于使用layui怎么實(shí)現(xiàn)一個(gè)多圖片上傳功能就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,可以學(xué)到更多知識(shí)。如果覺得文章不錯(cuò),可以把它分享出去讓更多的人看到。

當(dāng)前標(biāo)題:使用layui怎么實(shí)現(xiàn)一個(gè)多圖片上傳功能-創(chuàng)新互聯(lián)
分享鏈接:http://www.bm7419.com/article46/dgcihg.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供移動(dòng)網(wǎng)站建設(shè)動(dòng)態(tài)網(wǎng)站、關(guān)鍵詞優(yōu)化、App設(shè)計(jì)、外貿(mào)建站、微信公眾號(hào)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

成都網(wǎng)站建設(shè)