溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

使用layui怎么實現一個多圖片上傳功能

發布時間:2021-04-19 17:17:50 來源:億速云 閱讀:630 作者:Leah 欄目:web開發

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

//給圖片添加刪除
    function mouseChange() {
        $(document).on("mouseenter mouseleave", ".file-iteme", function (event) {
          if (event.type === "mouseenter") {
            //鼠標懸浮
            $(this).children(".info").fadeIn("fast");
            $(this).children(".handle").fadeIn("fast");
          } else if (event.type === "mouseleave") {
            //鼠標離開
            $(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(); //將每次選擇的文件追加到文件隊列
        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]; //刪除對應的文件
                  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]; //刪除文件隊列已經上傳成功的文件
          return;
        };
 
      }
    });

關于使用layui怎么實現一個多圖片上傳功能就分享到這里了,希望以上內容可以對大家有一定的幫助,可以學到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

亚洲午夜精品一区二区_中文无码日韩欧免_久久香蕉精品视频_欧美主播一区二区三区美女