1. 程式人生 > >layui實現文件或圖片上傳記錄

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

css pro 上傳 選擇框 UNC join 重傳 doc urn

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

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

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

var uploadListIns;

2.進行賦值

//多文件列表示例
/**
 * 圖片上傳
 */
layui.use(‘upload‘, function(){
    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(obj.total > obj.successful){
                layer.msg("有文件上傳失敗,暫不更新生產進度,請重試或聯系管理員");
            }else {
                //更新生產進度
                updateProductionSchedule(currentId, currentSchedule);
            }
        },
        error: function(index, upload){
            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.最終前端實現效果如下:

技術分享圖片

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