首頁 web前端 Layui教程 layui檔案上傳、預覽及修改方法

layui檔案上傳、預覽及修改方法

Dec 28, 2019 pm 05:13 PM
layui

layui檔案上傳、預覽及修改方法

單一檔案上傳

1、HTML

<blockquote class="layui-elem-quote layui-quote-nm" style="margin-top: 10px;">
                        <div id="uploadQRcode" class="layui-upload">
                        <button type="button" class="layui-btn" id="uploadQR">
                            <i class="layui-icon">&#xe67c;</i>上传客服二维码<span style="color: red;font-size: 20px;">*</span>
                        </button>
                        <div class="layui-upload-list">
                            <img id="qrshow" src="" alt="" class="layui-upload-img"
                                 style="height: 100px;width:100px;border:1px solid black;">
                        </div>
                        <div id="startDiv">
                            <button type="button" class="layui-btn" id="startUploadQR">开始上传</button>
                        </div>
                            <div style="color: #c2c2c2;margin:10px 0;">温馨提示: 每次最多上传一张图片, 单张图片的大小不超过2MB</div>
                    </div>
                    <input type="text" name="cli_qrcode" id="qrInput" style="display: none;" lay-verify="required">
</blockquote>
登入後複製

2、js部分

layui.use(['form', 'element', 'upload'], function () {
        var form = layui.form;
        var element = layui.element;
        var $ = layui.jquery;
        var upload = layui.upload;

        //单文件示例  选完文件后不自动上传
        var uploadSingle = upload.render({
            elem: '#uploadQR'
            , url: '/web/api/upload/upload?option=4'
            , accept: 'images'  // 允许上传的文件类型
            , size: 2048        // 最大允许上传的文件大小  单位 KB
            , auto: false
            , bindAction: '#startUploadQR'
            , choose: function (obj) {
                //预读本地文件示例,不支持ie8
                obj.preview(function (index, file, result) {
                    $('#qrshow').attr('src', result); //图片链接(base64)
                });
            }
            , done: function (res, index, upload) {
                if (res.code == 0) {
                    //上传成功
                    $("#qrInput").val(res.data[0].fp_relative);
                    var startDiv = $('#startDiv');
                    startDiv.html('<span style="color: #5FB878;">上传成功</span>');
                } else {
                    this.error(index, upload);
                }
            }
            , error: function (index, upload) {
                //演示失败状态,并实现重传
                var startDiv = $('#startDiv');
                startDiv.html('<span style="color: #FF5722;">上传失败</span> <a class="layui-btn layui-btn-xs demo-reload" style="width:50px;height:30px;text-align:center;line-height:30px;">重试</a>');
                startDiv.find('.demo-reload').on('click', function () {
                    uploadSingle.upload();
                });
            }
        });
    });
登入後複製

多重圖片的上傳

1、HTML

<blockquote class="layui-elem-quote layui-quote-nm" style="margin-top: 10px;">
                        <div id="uploadImg" class="layui-upload">
                        <button type="button" class="layui-btn" id="upload">
                            <i class="layui-icon">&#xe67c;</i>上传商铺图片<span style="color: red;font-size: 20px;">*</span>
                        </button>
                        <div class="layui-upload-list">
                            <table class="layui-table" style="text-align: center;">
                                <thead>
                                <tr>
                                    <th style="text-align: center;">图片预览</th>
                                    <th style="text-align: center;">大小</th>
                                    <th style="text-align: center;">状态</th>
                                    <th style="text-align: center;">操作</th>
                                </tr>
                                </thead>
                                <tbody id="imgList"></tbody>
                            </table>
                        </div>
                        <button type="button" class="layui-btn" id="startUpload">开始上传</button>
                            <div style="color: #c2c2c2;margin:10px 0;">温馨提示: 每次最多上传六张图片, 单张图片的大小不超过5MB, 长宽比例推荐1.5:1,
                                推荐上传图片长675px,宽450px
                            </div>
                    </div>
                    <input type="text" name="face_img" id="imgInput" style="display: none;" lay-verify="required">
</blockquote>
登入後複製

2、js部分

layui.use(['table', 'form', 'element', 'upload'], function () {
        var table = layui.table;
        var form = layui.form;
        var element = layui.element;
        var $ = layui.jquery;
        var upload = layui.upload;
        
        //多文件列表示例
        var demoListView = $('#imgList');
        var totalArray = new Array();
        var uploadInst = upload.render({
            elem: '#upload' //绑定元素
            , url: '/web/api/upload/upload?option=3' //上传接口
            , accept: 'images'  // 允许上传的文件类型
            // , acceptMime: 'image/jpg,image/png'   // (只支持jpg和png格式,多个用逗号隔开),
            , size: 5120        // 最大允许上传的文件大小  单位 KB
            , auto: false //选择文件后不自动上传
            , bindAction: '#startUpload' //指向一个按钮触发上传
            , multiple: true   // 开启多文件上传
            , number: 6    //  同时上传文件的最大个数
            , choose: function (obj) {
                var files = this.files = obj.pushFile(); //将每次选择的文件追加到文件队列
                var arr = Object.keys(files);
                totalArray = totalArray.concat(arr);
                // 检查上传文件的个数
                if (totalArray.length <= 6) {
                    //读取本地文件
                    obj.preview(function (index, file, result) {
                        var tr = $([&#39;<tr id="upload-&#39; + index + &#39;">'
                            , '<td><img src="&#39; + result + &#39;" alt="&#39; + file.name + &#39;" class="layui-upload-img" style="height: 66px;width:100px;"></td>'
                            , '<td>' + (file.size / 1014).toFixed(1) + 'kb</td>'
                            , '<td>等待上传</td>'
                            , '<td>'
                            , '<button class="layui-btn demo-reload layui-hide">重传</button>'
                            , '<button class="layui-btn 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);
                    });
                } else {
                    // 超出上传最大文件
                    layer.msg("上传文件最大不超过6个")
                }

            }
            , done: function (res, index, upload) {
                console.log("res", res);
                if (res.code == 0) { //上传成功
                    // 上传成功后将图片路径拼接到input中,多个路径用","分割
                    var inputVal = document.getElementById("imgInput").value;
                    var valData = "";
                    if (inputVal) {
                        valData = inputVal + "," + res.data[0].fp_relative;
                    } else {
                        valData = res.data[0].fp_relative;
                    }
                    document.getElementById("imgInput").value = valData;
                    var tr = demoListView.find('tr#upload-' + index)
                        , tds = tr.children();
                    tds.eq(2).html('<span style="color: #5FB878;">上传成功</span>');
                    tds.eq(3).html(''); //清空操作
                    return delete this.files[index]; //删除文件队列已经上传成功的文件

                }
                this.error(index, upload);
            }
            , 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'); //显示重传
            }
        });
    });
登入後複製

新增頁面

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>添加商铺</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="{{static_url(&#39;layui/css/layui.css&#39;)}}" media="all">
</head>
<body>
<div style="margin-top: 30px;">

    <form class="layui-form layui-form-pane">
        <div>
            <div class="layui-col-xs8 layui-col-xs-offset2">
                <div>
                    <label>商铺名称<span style="color: red;font-size: 20px;">*</span></label>
                    <div>
                        <input type="text" lay-verify="required" name="name" autocomplete="off">
                    </div>
                </div>
            </div>
        </div>

        <div>
            <div class="layui-col-xs8 layui-col-xs-offset2">
                <div>
                    <label>商铺编号</label>
                    <div>
                        <input type="text" name="code" autocomplete="off">
                    </div>
                </div>
            </div>
        </div>

        <div>
            <div class="layui-col-xs8 layui-col-xs-offset2">
                <div>
                    <label>详细地址<span style="color: red;font-size: 20px;">*</span></label>
                    <div>
                        <input type="text" name="address" required lay-verify="required" autocomplete="off"
                               class="layui-input">
                    </div>
                </div>
            </div>
        </div>
        <div>
            <div class="layui-col-xs8 layui-col-xs-offset2">
                <div>
                    <label>联系方式<span style="color: red;font-size: 20px;">*</span></label>
                    <div>
                        <input type="text" name="contact" required lay-verify="required|phone" autocomplete="off"
                               class="layui-input">
                    </div>
                </div>
            </div>
        </div>

        <div>
            <div class="layui-col-xs8 layui-col-xs-offset2">
                <div>
                    <blockquote class="layui-elem-quote layui-quote-nm" style="margin-top: 10px;">
                        <div id="uploadQRcode">
                        <button type="button" id="uploadQR">
                            <i>&#xe67c;</i>上传客服二维码<span style="color: red;font-size: 20px;">*</span>
                        </button>
                        <div>
                            <img id="qrshow" src="" alt=""
                                 style="height: 100px;width:100px;border:1px solid black;">
                        </div>
                        <div id="startDiv">
                            <button type="button" id="startUploadQR">开始上传</button>
                        </div>
                            <div style="color: #c2c2c2;margin:10px 0;">温馨提示: 每次最多上传一张图片, 单张图片的大小不超过2MB</div>
                    </div>
                    <input type="text" name="cli_qrcode" id="qrInput" style="display: none;" lay-verify="required">
                    </blockquote>

                </div>
            </div>
        </div>
        <div>
            <div class="layui-col-xs8 layui-col-xs-offset2">
                <div>
                    <blockquote class="layui-elem-quote layui-quote-nm" style="margin-top: 10px;">
                        <div id="uploadImg">
                        <button type="button" id="upload">
                            <i>&#xe67c;</i>上传商铺图片<span style="color: red;font-size: 20px;">*</span>
                        </button>
                        <div>
                            <table style="text-align: center;">
                                <thead>
                                <tr>
                                    <th style="text-align: center;">图片预览</th>
                                    <th style="text-align: center;">大小</th>
                                    <th style="text-align: center;">状态</th>
                                    <th style="text-align: center;">操作</th>
                                </tr>
                                </thead>
                                <tbody id="imgList"></tbody>
                            </table>
                        </div>
                        <button type="button" id="startUpload">开始上传</button>
                            <div style="color: #c2c2c2;margin:10px 0;">温馨提示: 每次最多上传六张图片, 单张图片的大小不超过5MB, 长宽比例推荐1.5:1,
                                推荐上传图片长675px,宽450px
                            </div>
                    </div>
                    <input type="text" name="face_img" id="imgInput" style="display: none;" lay-verify="required">
                    </blockquote>

                </div>
            </div>
        </div>
        <div>
            <div class="layui-col-xs8 layui-col-xs-offset2" style="margin-top: 30px;">

                <div>
                    <button class="layui-btn layui-btn-fluid" lay-submit="" lay-filter="addObject">确认保存</button>
                </div>
            </div>
        </div>
    </form>


</div>

<script src="{{static_url(&#39;layui/layui.js&#39;)}}" charset="utf-8"></script>
<script>
    layui.use(['table', 'form', 'element', 'upload'], function () {
        var table = layui.table;
        var form = layui.form;
        var element = layui.element;
        var $ = layui.jquery;
        var upload = layui.upload;

        //单文件示例  选完文件后不自动上传
        var uploadSingle = upload.render({
            elem: '#uploadQR'
            , url: '/web/api/upload/upload?option=4'
            , accept: 'images'  // 允许上传的文件类型
            , size: 2048        // 最大允许上传的文件大小  单位 KB
            , auto: false
            , bindAction: '#startUploadQR'
            , choose: function (obj) {
                //预读本地文件示例,不支持ie8
                obj.preview(function (index, file, result) {
                    $('#qrshow').attr('src', result); //图片链接(base64)
                });
            }
            , done: function (res, index, upload) {
                if (res.code == 0) {
                    //上传成功
                    $("#qrInput").val(res.data[0].fp_relative);
                    var startDiv = $('#startDiv');
                    startDiv.html('<span style="color: #5FB878;">上传成功</span>');
                } else {
                    this.error(index, upload);
                }
            }
            , error: function (index, upload) {
                //演示失败状态,并实现重传
                var startDiv = $('#startDiv');
                startDiv.html('<span style="color: #FF5722;">上传失败</span> <a class="layui-btn layui-btn-xs demo-reload" style="width:50px;height:30px;text-align:center;line-height:30px;">重试</a>');
                startDiv.find('.demo-reload').on('click', function () {
                    uploadSingle.upload();
                });
            }
        });

        //多文件列表示例
        var demoListView = $('#imgList');
        var totalArray = new Array();
        var uploadInst = upload.render({
            elem: '#upload' //绑定元素
            , url: '/web/api/upload/upload?option=3' //上传接口
            , accept: 'images'  // 允许上传的文件类型
            // , acceptMime: 'image/jpg,image/png'   // (只支持jpg和png格式,多个用逗号隔开),
            , size: 5120        // 最大允许上传的文件大小  单位 KB
            , auto: false //选择文件后不自动上传
            , bindAction: '#startUpload' //指向一个按钮触发上传
            , multiple: true   // 开启多文件上传
            , number: 6    //  同时上传文件的最大个数
            , choose: function (obj) {
                var files = this.files = obj.pushFile(); //将每次选择的文件追加到文件队列
                var arr = Object.keys(files);
                totalArray = totalArray.concat(arr);
                // 检查上传文件的个数
                if (totalArray.length <= 6) {
                    //读取本地文件
                    obj.preview(function (index, file, result) {
                        var tr = $([&#39;<tr id="upload-&#39; + index + &#39;">'
                            , '<td><img src="&#39; + result + &#39;" alt="&#39; + file.name + &#39;" style="height: 66px;width:100px;"></td>'
                            , '<td>' + (file.size / 1014).toFixed(1) + 'kb</td>'
                            , '<td>等待上传</td>'
                            , '<td>'
                            , '<button class="layui-btn demo-reload layui-hide">重传</button>'
                            , '<button class="layui-btn 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);
                    });
                } else {
                    // 超出上传最大文件
                    layer.msg("上传文件最大不超过6个")
                }

            }
            , done: function (res, index, upload) {
                console.log("res", res);
                if (res.code == 0) { //上传成功
                    // 上传成功后将图片路径拼接到input中,多个路径用","分割
                    var inputVal = document.getElementById("imgInput").value;
                    var valData = "";
                    if (inputVal) {
                        valData = inputVal + "," + res.data[0].fp_relative;
                    } else {
                        valData = res.data[0].fp_relative;
                    }
                    document.getElementById("imgInput").value = valData;
                    var tr = demoListView.find('tr#upload-' + index)
                        , tds = tr.children();
                    tds.eq(2).html('<span style="color: #5FB878;">上传成功</span>');
                    tds.eq(3).html(''); //清空操作
                    return delete this.files[index]; //删除文件队列已经上传成功的文件

                }
                this.error(index, upload);
            }
            , 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'); //显示重传
            }
        });
        form.on("submit(addObject)", function (data) {
            $.ajax({
                url: "/web/api/seller/add",
                type: "post",
                data: data.field,
                dataType: "json",
                success: function (response) {
                    if (response["code"] == 0) {
                        layer.msg("添加成功", {
                            icon: 1,
                            time: 2500 // 默认三秒
                        }, function () { // 关闭回调,关闭层刷新页面
                            var index = parent.layer.getFrameIndex(window.name);  // 先得到当前iframe层的索引
                            parent.layer.close(index);
                        });
                    } else {
                        layer.msg(response["msg"], {
                            icon: 1,
                            time: 1500 // 1.5秒,默认三秒
                        });
                    }
                    return false;
                },
                error: function (response) {
                    layer.msg(response["msg"], {
                        icon: 1,
                        time: 1500 // 1.5秒,默认三秒
                    });
                }
            });

            return false; // 关闭表单提交
        });
    });
</script>
</body>
</html>
登入後複製

編輯頁面

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>编辑商铺</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="{{static_url(&#39;layui/css/layui.css&#39;)}}" media="all">
</head>
<body>
<div style="margin-top: 30px;">
    <form class="layui-form layui-form-pane" lay-filter="updateForm">
        <input type="text" name="seller_id" lay-verify="required" style="display: none;">
        <div>
            <div class="layui-col-xs10 layui-col-xs-offset1">
                <div>
                    <label>商铺名称</label>
                    <div>
                        <input type="text" lay-verify="required" name="name" autocomplete="off">
                    </div>
                </div>
            </div>
        </div>
        <div>
            <div class="layui-col-xs10 layui-col-xs-offset1">
                <div>
                    <label>商铺编号</label>
                    <div>
                        <input type="text" name="code" autocomplete="off">
                    </div>
                </div>
            </div>
        </div>
        <div>
            <div class="layui-col-xs10 layui-col-xs-offset1">
                <div>
                    <label>详细地址</label>
                    <div>
                        <input type="text" name="address" required lay-verify="required" autocomplete="off"
                               class="layui-input">
                    </div>
                </div>
            </div>
        </div>
        <div>
            <div class="layui-col-xs10 layui-col-xs-offset1">
                <div>
                    <label>联系方式</label>
                    <div>
                        <input type="text" name="contact" required lay-verify="required|phone" autocomplete="off"
                               class="layui-input">
                    </div>
                </div>
            </div>
        </div>

        <div>
            <div class="layui-col-xs10 layui-col-xs-offset1">
                <div>
                    <blockquote class="layui-elem-quote layui-quote-nm" style="margin-top: 10px;">
                        <div id="uploadQRcode">
                            <div>
                                <img id="qrshow" src="" alt=""
                                     style="height: 100px;width:100px;border:1px solid black;">
                            </div>
                            <div>
                                <div>
                                    <button type="button" class="layui-btn layui-btn-sm" id="uploadQR">
                                        <i>&#xe67c;</i>修改客服二维码
                                    </button>
                                </div>
                                <div id="startDiv">
                                    <button type="button" class="layui-btn layui-hide layui-btn-sm" id="startUploadQR"
                                            style="margin-left: 30px;">开始上传
                                    </button>
                                </div>
                            </div>
                            <div style="color: #c2c2c2;margin:10px 0;">温馨提示: 每次最多上传一张图片, 单张图片的大小不超过2MB</div>
                        </div>
                        <input type="text" name="old_cli_qrcode" style="display: none;">
                        <input type="text" name="cli_qrcode" id="qrInput" lay-verify="required" style="display: none;">
                    </blockquote>
                </div>
            </div>
        </div>
        <div>
            <div class="layui-col-xs10 layui-col-xs-offset1">
                <div>
                    <div id="uploadImg">
                        <div>
                            <table style="text-align: center;">
                                <thead>
                                <tr>
                                    <th style="text-align: center;">图片预览</th>
                                    <th style="text-align: center;">状态</th>
                                    <th style="text-align: center;">操作</th>
                                </tr>
                                </thead>
                                <tbody id="imgList"></tbody>
                            </table>
                        </div>
                        <div>
                            <div>
                                <button type="button" class="layui-btn layui-btn-sm" id="upload">
                                    <i>&#xe67c;</i>添加商铺图片
                                </button>
                            </div>
                            <div class="layui-col-xs3 layui-col-xs-offset5">
                                <button type="button" class="layui-btn layui-btn-fluid layui-btn-sm" id="startUpload">
                                    开始上传
                                </button>
                            </div>
                        </div>
                        <div style="color: #c2c2c2;margin:10px 0;">温馨提示: 每次最多上传六张图片, 单张图片的大小不超过5MB, 长宽比例推荐1.5:1,
                            推荐上传图片长675px,宽450px
                        </div>
                    </div>
                    <input type="text" name="old_face_img" style="display: none;">
                    <input type="text" name="face_img" id="imgInput" lay-verify="required" style="display: none;">
                </div>
            </div>
        </div>
        <div>
            <div class="layui-col-xs10 layui-col-xs-offset1" style="margin-top: 30px;">
                <div>
                    <button class="layui-btn layui-btn-fluid" lay-submit="" lay-filter="addObject">确认保存</button>
                </div>
            </div>
        </div>
    </form>
</div>

<script src="{{static_url(&#39;layui/layui.js&#39;)}}" charset="utf-8"></script>
<!-- 注意:如果你直接复制所有代码到本地,上述js路径需要改成你本地的 -->

<script>
    layui.use(['form', 'element', 'jquery', 'upload'], function () {
        var form = layui.form;
        var element = layui.element;
        var $ = layui.jquery;
        var upload = layui.upload;

        //单文件示例  选完文件后不自动上传
        var uploadSingle = upload.render({
            elem: '#uploadQR'
            , url: '/web/api/upload/upload?option=4'
            , accept: 'images'  // 允许上传的文件类型
            , size: 2048        // 最大允许上传的文件大小  单位 KB
            , auto: false
            , bindAction: '#startUploadQR'
            , choose: function (obj) {
                //预读本地文件示例,不支持ie8
                obj.preview(function (index, file, result) {
                    $('#qrshow').attr('src', result); //图片链接(base64)
                });
            }
            , done: function (res, index, upload) {
                if (res.code == 0) {
                    //上传成功
                    $("#qrInput").val(res.data[0].fp_relative);
                    $("#uploadQR").addClass("layui-hide");
                    var startDiv = $('#startDiv');
                    startDiv.html('<span style="color: #5FB878;font-size: 17px;">修改成功</span>');
                } else {
                    this.error(index, upload);
                }
            }
            , error: function (index, upload) {
                //演示失败状态,并实现重传
                var startDiv = $('#startDiv');
                startDiv.html('<span style="color: #FF5722;">上传失败</span> <a class="layui-btn layui-btn-xs demo-reload" style="width:50px;height:30px;text-align:center;line-height:30px;">重试</a>');
                startDiv.find('.demo-reload').on('click', function () {
                    uploadSingle.upload();
                });
            }
        });

        //多文件列表示例
        var demoListView = $('#imgList');
        var totalArray = new Array();
        var uploadInst = upload.render({
            elem: '#upload' //绑定元素
            , url: '/web/api/upload/upload?option=3' //上传接口
            , accept: 'images'  // 允许上传的文件类型
            // , acceptMime: 'image/jpg,image/png'   // (只支持jpg和png格式,多个用逗号隔开),
            , size: 5120        // 最大允许上传的文件大小  单位 KB
            , auto: false //选择文件后不自动上传
            , bindAction: '#startUpload' //指向一个按钮触发上传
            , multiple: true   // 开启多文件上传
            , number: 6    //  同时上传文件的最大个数
            , choose: function (obj) {
                var files = this.files = obj.pushFile(); //将每次选择的文件追加到文件队列
                var totalArray = Object.keys(files);
                // 检查上传文件的个数
                if (totalArray.length < (7 - demoListView.data("choiceTotal"))) {
                    //读取本地文件
                    obj.preview(function (index, file, result) {
                        var tr = $([&#39;<tr id="upload-&#39; + index + &#39;">'
                            , '<td><img src="&#39; + result + &#39;" alt="&#39; + file.name + &#39;" style="height: 66px;width:100px;"></td>'
                            , '<td>等待上传</td>'
                            , '<td>'
                            , '<button class="layui-btn layui-btn-sm demo-reload layui-hide">重传</button>'
                            , '<button class="layui-btn layui-btn-sm 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();
                            uploadInst.config.elem.next()[0].value = ''; //清空 input file 值,以免删除后出现同名文件不可选
                            totalArray.splice(totalArray.indexOf(index), 1);
                        });
                        demoListView.append(tr);
                    });
                } else {
                    // 超出上传最大文件
                    layer.msg("上传文件最大不超过6个");
                    totalArray.pop();
                }
            }
            , done: function (res, index, upload) {
                if (res.code == 0) { //上传成功
                    // 上传成功后将图片路径拼接到input中,多个路径用","分割
                    var inputVal = document.getElementById("imgInput").value;
                    var valData = "";
                    if (inputVal) {
                        valData = inputVal + "," + res.data[0].fp_relative;
                    } else {
                        valData = res.data[0].fp_relative;
                    }
                    document.getElementById("imgInput").value = valData;
                    var tr = demoListView.find('tr#upload-' + index)
                        , tds = tr.children();
                    tds.eq(1).html('<span style="color: #5FB878;">上传成功</span>');
                    tds.eq(2).html(''); //清空操作
                    return delete this.files[index]; //删除文件队列已经上传成功的文件
                }
                this.error(index, upload);
            }
            , error: function (index, upload) {
                var tr = demoListView.find('tr#upload-' + index)
                    , tds = tr.children();
                tds.eq(1).html('<span style="color: #FF5722;">上传失败</span>');
                tds.eq(2).find('.demo-reload').removeClass('layui-hide'); //显示重传
            }
        });

        // 监听修改客服二维码事件
        $("#uploadQR").on("click", function () {
            $("#startUploadQR").removeClass('layui-hide');
        });

        // 处理图片的修改
        demoListView.on('click', '.edit-btn', function () {
            var csid = $(this).attr("csid");
            var startid = $(this).attr("startid");
            var currentIndex = parseInt(csid.split("_")[1]);
            var $currentTr = $(this).parent().parent();
            $(this).addClass("layui-hide");
            $("#" + csid).removeClass("layui-hide");
            $("#" + startid).removeClass("layui-hide");
            var uploadEdit = upload.render({
                elem: '#' + csid
                , url: '/web/api/upload/upload?option=3'
                , accept: 'images'  // 允许上传的文件类型
                , size: 2048        // 最大允许上传的文件大小  单位 KB
                , auto: false
                , bindAction: '#' + startid
                , choose: function (obj) {
                    //预读本地文件示例,不支持ie8
                    obj.preview(function (index, file, result) {
                        $currentTr.children().eq(0).html('<img src="&#39; + result + &#39;" alt="" style="height: 66px;width:100px;">') //图片链接(base64)
                    });
                }
                , done: function (res, index, upload) {
                    if (res.code == 0) {
                        //上传成功
                        var InputTag = $("#imgInput");
                        var oldInputStrList = InputTag.val().split(",");
                        oldInputStrList[currentIndex] = res.data[0].fp_relative;
                        var newInputStr = oldInputStrList.join(",");
                        InputTag.val(newInputStr);
                        $currentTr.children().eq(1).text("修改成功");
                        $currentTr.children().eq(2).html("");
                    } else {
                        this.error(index, upload);
                    }
                }
                , error: function (index, upload) {
                    //演示失败状态,并实现重传
                    var fileHtml = '<span style="color: #FF5722;">上传失败</span> <a class="layui-btn layui-btn-xs demo-reload" style="width:50px;height:30px;text-align:center;line-height:30px;">重试</a>';
                    $currentTr.children().eq(2).html(fileHtml);
                    $currentTr.find('.demo-reload').on('click', function () {
                        uploadEdit.upload();
                    });
                }
            });
        });

        // 处理图片的删除
        demoListView.on("click", '.delete-btn', function () {
            var delid = this.id;
            var currentDelIndex = parseInt(delid.split("_")[1]);
            var theCurrentTr = $(this).parent().parent();
            // 更新表格中当前行后面每一行的序号
            // 找到当前行后面的每一行
            theCurrentTr.nextAll().each(function () {
                // each中的this和外面的this不一样了!
                var oldCsid = $(this).children().eq(2).find(".edit-btn").attr("csid");
                var oldStarid = $(this).children().eq(2).find(".edit-btn").attr("startid");
                var oldChoiceid = $(this).children().eq(2).find(".choice-btn").attr("id");
                var oldUploadid = $(this).children().eq(2).find(".upload-btn").attr("id");
                var oldDelid = $(this).children().eq(2).find(".delete-btn").attr("id");
                if (oldDelid && oldCsid) {
                    $(this).children().eq(2).find(".edit-btn").attr("csid", oldCsid.split("_")[0] + "_" + (oldCsid.split("_")[1] - 1));
                    $(this).children().eq(2).find(".edit-btn").attr("startid", oldStarid.split("_")[0] + "_" + (oldStarid.split("_")[1] - 1));
                    $(this).children().eq(2).find(".choice-btn").attr("id", oldChoiceid.split("_")[0] + "_" + (oldChoiceid.split("_")[1] - 1));
                    $(this).children().eq(2).find(".upload-btn").attr("id", oldUploadid.split("_")[0] + "_" + (oldUploadid.split("_")[1] - 1));
                    $(this).children().eq(2).find(".delete-btn").attr("id", oldDelid.split("_")[0] + "_" + (oldDelid.split("_")[1] - 1));
                }
            });
            // 找到当前行,删除
            theCurrentTr.remove();
            // 修改新的input框中的值
            var delInputTag = $("#imgInput");
            var oldDelInputStrList = delInputTag.val().split(",");
            oldDelInputStrList.splice(currentDelIndex, 1);
            var delnewInputStr = oldDelInputStrList.join(",");
            delInputTag.val(delnewInputStr);
            // 修改全局可上传文件的总个数
            var $currentTotal = demoListView.data("choiceTotal");
            demoListView.data("choiceTotal", $currentTotal - 1);
        });

        // 填充管理员详情
        $.ajax({
            url: "/web/api/seller/detail?seller_id={{seller_id}}",
            type: "get",
            dataType: "json",
            success: function (response) {
                console.log(response);
                $("#qrshow").attr("src", response.data.qrcode_url);
                var faceList = response.data.face_url_list;
                demoListView.data("choiceTotal", faceList.length);
                if (faceList) {
                    for (var i = 0; i < faceList.length; i++) {
                        var trEle = document.createElement("tr");
                        var trHtml = '<td><img src="&#39; + faceList[i] + &#39;" alt="" style="height: 66px;width:100px;"></td>'
                            + '<td>等待修改</td><td><button type="button" class="layui-btn layui-btn-sm edit-btn" csid="choice_&#39; + i
                            + &#39;" startid="startUpload_&#39; + i + &#39;">修改</button><button type="button" class="layui-btn layui-btn-normal layui-btn-sm layui-hide choice-btn" id="choice_&#39; + i
                            + &#39;" style="margin-right: 10px;">选择图片</button><button type="button" class="layui-btn layui-btn-sm layui-hide upload-btn" id="startUpload_&#39; + i + &#39;">上传</button>' +
                            '<button type="button" class="layui-btn layui-btn-sm delete-btn" id="delete_&#39; + i + &#39;">删除</button></td>';
                        trEle.innerHTML = trHtml;
                        $("#imgList").append(trEle);
                    }
                }
                form.val("updateForm", {
                    "seller_id": response.data.id,
                    "name": response.data.name,
                    "code": response.data.code,
                    "address": response.data.address,
                    "contact": response.data.contact,
                    "cli_qrcode": response.data.cli_qrcode,
                    "old_cli_qrcode": response.data.cli_qrcode,
                    "face_img": response.data.face_img,
                    "old_face_img": response.data.face_img
                });
                form.render();
            }
        });

        // 绑定提交事件
        form.on("submit(addObject)", function (data) {
            $.ajax({
                url: "/web/api/seller/update",
                type: "post",
                data: data.field,
                dataType: "json",
                success: function (response) {
                    if (response["code"] == 0) {
                        layer.msg("更新成功", {
                            icon: 1,
                            time: 1500 // 1.5秒,默认三秒
                        }, function () { // 关闭回调,关闭层刷新页面
                            var index = parent.layer.getFrameIndex(window.name);  // 先得到当前iframe层的索引
                            parent.layer.close(index);
                        });
                    } else {
                        layer.msg(response["msg"], {
                            icon: 1,
                            time: 1500 // 1.5秒,默认三秒
                        });
                    }

                    return false;
                },
                error: function (response) {
                    layer.msg(response["msg"], {
                        icon: 1,
                        time: 1500 // 1.5秒,默认三秒
                    });
                    return false;
                }
            });

            return false; // 关闭表单提交(注意:此处不能省略,此处不能省略,此处不能省略。。。 否则页面刷新有问题)
        });
    });


</script>
</body>
</html>
登入後複製

更多layui知識請關注 layui使用教程欄位。

以上是layui檔案上傳、預覽及修改方法的詳細內容。更多資訊請關注PHP中文網其他相關文章!

本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

AI Hentai Generator

AI Hentai Generator

免費產生 AI 無盡。

熱門文章

R.E.P.O.能量晶體解釋及其做什麼(黃色晶體)
1 個月前 By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.最佳圖形設置
1 個月前 By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.如果您聽不到任何人,如何修復音頻
1 個月前 By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.聊天命令以及如何使用它們
1 個月前 By 尊渡假赌尊渡假赌尊渡假赌

熱工具

記事本++7.3.1

記事本++7.3.1

好用且免費的程式碼編輯器

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

禪工作室 13.0.1

禪工作室 13.0.1

強大的PHP整合開發環境

Dreamweaver CS6

Dreamweaver CS6

視覺化網頁開發工具

SublimeText3 Mac版

SublimeText3 Mac版

神級程式碼編輯軟體(SublimeText3)

layui登陸頁怎麼設定跳轉 layui登陸頁怎麼設定跳轉 Apr 04, 2024 am 03:12 AM

layui 登入頁面跳轉設定步驟:新增跳轉代碼:在登入表單提交按鈕點選事件中新增判斷,成功登入後透過 window.location.href 跳到指定頁面。修改 form 配置:在 lay-filter="login" 的 form 元素中新增 hidden 輸入字段,name 為 "redirect",value 為目標頁面位址。

layui怎麼取得表單數據 layui怎麼取得表單數據 Apr 04, 2024 am 03:39 AM

layui 提供了多種取得表單資料的方法,包括直接取得表單所有欄位資料、取得單一表單元素值、使用formAPI.getVal() 方法取得指定欄位值、將表單資料序列化並作為AJAX 請求參數,以及監聽表單提交事件獲取資料。

layui如何實現自適應 layui如何實現自適應 Apr 26, 2024 am 03:00 AM

透過使用layui框架的響應式佈局功能,可以實現自適應佈局。步驟包括:引用layui框架。定義自適應佈局容器,設定layui-container類別。使用響應式斷點(xs/sm/md/lg)隱藏特定斷點下的元素。利用網格系統(layui-col-)指定元素寬度。透過偏移量(layui-offset-)建立間距。使用響應式實用工具(layui-invisible/show/block/inline)控制元素的可見性和顯示方式。

layui怎麼傳數據 layui怎麼傳數據 Apr 26, 2024 am 03:39 AM

使用 layui 傳輸資料的方法如下:使用 Ajax:建立請求對象,設定請求參數(URL、方法、資料),處理回應。使用內建方法:使用 $.post、$.get、$.postJSON 或 $.getJSON 等內建方法簡化資料傳輸。

layui跟vue有啥差別 layui跟vue有啥差別 Apr 04, 2024 am 03:54 AM

layui與Vue的差異主要體現在功能和關注點上。 layui專注於快速開發UI元素,提供預製元件簡化頁面建置;而Vue則是全端框架,注重資料綁定、元件化開發和狀態管理,更適合建構複雜應用程式。 layui學習簡單,適合快速建立頁面;Vue學習曲線陡峭,但有助於建立可擴展且易於維護的應用程式。根據專案需求和開發者技能水平,可以選擇合適的框架。

layui怎麼運行 layui怎麼運行 Apr 04, 2024 am 03:42 AM

若要執行 layui,請執行以下步驟:1. 匯入 layui 腳本;2. 初始化 layui;3. 使用 layui 元件;4. 匯入 layui 樣式(可選);5. 確保腳本相容並注意其他注意事項。透過這些步驟,您就可以使用 layui 的強大功能來建立 web 應用程式。

layui是什麼意思啊 layui是什麼意思啊 Apr 04, 2024 am 04:33 AM

layui是一個前端UI框架,它提供了豐富的UI元件、工具和功能,幫助開發人員快速建立現代化、響應式和互動式Web應用程序,特點包括:靈活輕量、模組化設計、豐富的元件、強大的工具和易於自訂。它廣泛應用於各種Web應用程式的開發中,包括管理系統、電商平台、內容管理系統、社交網路和行動裝置應用程式。

layui框架和vue框架的區別 layui框架和vue框架的區別 Apr 26, 2024 am 01:27 AM

layui和vue是前端框架,layui是一種輕量級的函式庫,提供UI元件和工具;vue是一個全面的框架,提供UI元件、狀態管理、資料綁定和路由等功能。 layui基於模組化的架構,vue是基於組件化的架構。 layui擁有較小的生態系統,vue擁有龐大且活躍的生態系統。 layui學習曲線較低,vue學習曲線較陡。 layui適用於小型專案和快速開發UI元件,vue適用於大型專案和需要豐富功能的場景。

See all articles