layui實現文件或圖片上傳記錄


首先是layui自己的官網關於圖片/文件上傳的幫助文檔:https://www.layui.com/doc/modules/upload.html

接下來是我自己的使用記錄:

1.首先在js中定義一個全局變量

var uploadListIns;

2.進行賦值

//多文件列表示例
/**
 * 圖片上傳
 */
layui.use('upload', function(){
var allSuccess = 1; var $ = layui.jquery,upload = layui.upload; var demoListView = $('#proImageList'); uploadListIns = upload.render({ elem: '#chooseFile', //選擇文件的按鈕 url: 'upload!ftp.action', //后台處理文件長傳的方法 data:{'serviceName':'外協訂單供應商上傳檢驗報告','tableName':'T_OUTSOURCE_ORDER','fileType':'圖片'}, accept: 'file', multiple: true, //是否允許多文件上傳 acceptMime: 'image/*', //規定打開文件選擇框時,篩選出的文件類型 field:'upload', auto: false, bindAction: '#upload', //用來觸發上傳的按鈕ID choose: function(obj){ //選擇文件后的回調函數,本例中在此將選擇的文件進行展示 var files = this.files = obj.pushFile(); //將每次選擇的文件追加到文件隊列 //讀取本地文件 obj.preview(function(index, file, result){ var tr = $(['<tr id="upload-'+ index +'">' ,'<td>'+ file.name +'</td>' ,'<td>'+ (file.size/1014).toFixed(1) +'kb</td>' ,'<td>等待上傳</td>' ,'<td>' ,'<button class="layui-btn layui-btn-xs demo-reload layui-hide">重傳</button>' ,'<button class="layui-btn layui-btn-xs layui-btn-danger demo-delete">刪除</button>' ,'</td>' ,'</tr>'].join('')); //單個重傳 tr.find('.demo-reload').on('click', function(){ obj.upload(index, file); }); //刪除 tr.find('.demo-delete').on('click', function(){ delete files[index]; //刪除對應的文件 tr.remove(); uploadListIns.config.elem.next()[0].value = ''; //清空 input file 值,以免刪除后出現同名文件不可選 }); demoListView.append(tr); }); }, done: function(res, index, upload){ //多文件上傳時,只要有一個文件上傳成功后就會觸發這個回調函數 console.info(res); if(res.status == "success"){ //上傳成功 var tr = demoListView.find('tr#upload-'+ index) ,tds = tr.children(); tds.eq(2).html('<span style="color: #5FB878;">上傳成功</span>'); tds.eq(3).html('<a href="'+res.url+'">查看</a>'); //清空操作 return delete this.files[index]; //刪除文件隊列已經上傳成功的文件 }else{ alert(res.message); } this.error(index, upload); }, allDone: function(obj){ //當文件全部被提交后,才觸發
        if(allSuccess != 1){ //if(obj.total > obj.successful){ 本來是用的這個做法,后來發現obj.successful是指請求成功的文件數,我有一次上傳一個文件失敗的時候發現這個值居然也是1,所以就不采用這個做法了,換成在error回調函數中對allSuccess進行賦值 layer.msg("有文件上傳失敗,暫不更新生產進度,請重試或聯系管理員"); }else { //更新生產進度 updateProductionSchedule(currentId, currentSchedule); } }, error: function(index, upload){
allsuccess = 0; var tr = demoListView.find('tr#upload-'+ index) ,tds = tr.children(); tds.eq(2).html('<span style="color: #FF5722;">上傳失敗</span>'); tds.eq(3).find('.demo-reload').removeClass('layui-hide'); //顯示重傳 } }); $(".layui-upload-file").hide(); });

  

上述js代碼中出現的相關html元素如下,相關引入js文件和css為:bootstrap3的js和css及layui的js文件即可

<!-- 模態框(Modal) -->
<div class="modal fade" id="uploadModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">
                    ×
                </button>
                <h4 class="modal-title" id="myModalLabel">
                    上傳檢驗報告
                </h4>
            </div>
            <div class="modal-body">
                <button type="button" class="btn btn-primary" id="chooseFile">選擇多文件</button>
                <button type="button" class="btn btn-success" id="upload">開始上傳</button>
                <div class="table-responsive">
                    <table class="table table-hover">
                        <thead><tr>
                            <th>文件名</th>
                            <th>大小</th>
                            <th>狀態</th>
                            <th>操作</th>
                        </tr></thead>
                        <tbody id="proImageList"></tbody>
                    </table>
                </div>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">關閉
                </button>
            </div>
        </div><!-- /.modal-content -->
    </div><!-- /.modal -->
</div>

  

3.在打開模態框時可以對1中定義的變量進行動態賦值,這些變量會相應的傳到后台中:

function showUploadModal(id) {
    //動態賦值
    uploadListIns.config.data.tableRecordId = id;
    uploadListIns.config.data.filenamePrefix = id+".自檢pass.";
    $("#uploadModal").modal("show");
}

4.最終前端實現效果如下:


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM