溫馨提示×

溫馨提示×

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

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

layui使用upload組件實現(xiàn)文件上傳功能

發(fā)布時間:2020-05-22 17:25:25 來源:億速云 閱讀:1760 作者:鴿子 欄目:web開發(fā)

背景:頁面上一個按鈕,點擊彈出上傳框,從按鈕的方法代碼開始寫:處理未選擇文件阻止上傳;通過判斷選擇文件的數(shù)量,顯示或隱藏上傳按鈕;

在js中定義:

function  uploadFile(){
    layer.open({
        type:1,
        title:'上傳文件‘,
        area:['25%','400px'],
        content:'<div class="layui-form-item" style="margin-top:40px;">\
          <div class="layui-input-block">\
            <input class="layui-btn" type="button" id="chooseFIle" value="選擇文件">\
            <span></span>\
          </div>\
        </div>\
          <div class="layui-form-item" style="margin-top:40px;">\
          <div class="layui-input-block">\
            <input class="layui-btn" type="button" id="uploadbtn" value="上傳">\
            <span></span>\
          </div>\
        </div>‘,
    btn:['關閉'],
    btn1:function(idx,ele){
        layer.closeAll();
      }
  })
createUpload();
}
 
var  files ;
function createUpload(){
      $("#uploadbtn").hide();
      $("#chooseFile").next().next("span").text("");
      layui.use(['upload'],function(){
        var uploadInst = upload.render({
            elem:'#chooseFile',
             url:' ',
              accept:'file',
             auto:false,
            multiple:true,
          acceptMime:'application/vnd.ms-excel,application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
          exts:'xls|xlsx',
          size:1024000,
          number:5,
          bindAction:'#uploadbtn',
          choose:function(obj){
            files = this.files = boj.pushFile();
            if(Object.keys(files).length>0){ $("#uploadbtn").show(); }
            obj.preview(function(index,file,result){
              $("#chooseFile").siblings("span").append("<div title='"+index+"'>"+file.name+"&nbsp;&nbsp;<span onclick='deletefile(\""+index+"\")'>&times;</span></div>")
               if(index>0) {$("#uploadbtn").show() ;}
              })
          },
        allDone:function(obj){  
            if(obj.successful){
              layer.msg(obj.total+"個文件上傳成功!");
            }
          },
        error:function(){
          layer.alert("上傳成功!");
        }
    })
  }
}
function  deletefile(index){
  delete  files[index];
  $("#chooseFile").siblings("span").find("div[title="+index+"]").remove();
  if(!Object.keys(files).length>0){
      $("#uploadbtn").hide();
  }
 
}

以上就是layui的upload組件使用和上傳阻止的詳細內(nèi)容,更多請關注億速云其它相關文章!

向AI問一下細節(jié)

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

AI