layui实现文件或图片上传记录

首先是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.最终前端实现效果如下:

原文地址:https://www.cnblogs.com/panlongfeng/p/9548074.html

时间: 2024-08-28 08:54:59

layui实现文件或图片上传记录的相关文章

链接ftp,把文件或图片上传到ftp指定的文件夹中

/******************************************************************** *  * * Filename : .java * Author :  * Date : 2015年6月5日 * Version : V1.00 * Description : * * History : Modify Id | Date | Origin | Description *************************************

aps.net mvc webapi 实现文件或图片上传

前几天看到网上有很多复杂的实现方式,觉得没必要,所以就写个简单的实现. 一:首先来看看Api Controller里面的代码: HttpContext.Current.Request.Files  这是一个文件集合对象,你客户端上载的所有文件都在这个集合当中 图中提供了2种方式获取单个文件对象,你可以按需使用,单个文件可以直接用下标,多个文件用name,例如: HttpPostedFile file =HttpContext.Current.Request.Files[0]; 图中HttpPos

ifram+form方式实现文件、图片上传、预览

1.前端代码 <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> <style> .btn{ display: inline-block; padding: 5px 10px; background-color: coral; color: white; } </

单个文件或图片上传(java)

public void uploadFile(){ UploadFile file = getFile("batfile", "images"); File source = file.getFile(); String fileName = file.getFileName(); String extension = fileName.substring(fileName.lastIndexOf(".")); String prefix; if

js文件、图片上传(原生方法和jquery的ajax两种都有)

<!DOCTYPE html><html> <head> <title>test</title> </head> <body> <input type="file" id="fileUpload"> <button id="submit">点击上传文件</button> <script src="javascri

服务器基于PHP CodeIgniter,Android基于Volley实现多文件/图片上传(含服务器,web版和android客户端完整代码)

问题背景:app在上传图片时,同时传递参数,支持传递多个图片.本文中的环境默认已经配好了服务器的CodeIgniter框架.事实上不使用这个框架也是可以的. 一,服务器部分 1,在controllers下的helpers新建文件upload_helper.php <?php /** * Make multifile array input complaint with CI_Upload.<br> * For use files[ ] input name you must use it

PHP 图片上传工具类(支持多文件上传)

====================ImageUploadTool======================== <?php class ImageUploadTool { private $file; //文件信息 private $fileList; //文件列表 private $inputName; //标签名称 private $uploadPath; //上传路径 private $fileMaxSize; //最大尺寸 private $uploadFiles; //上传文件

WebApi2 文件图片上传下载

Asp.Net Framework webapi2 文件上传与下载 前端界面采用Ajax的方式执行 一.项目结构 1.App_Start配置了跨域访问,以免请求时候因跨域问题不能提交.具体的跨域配置方式如下,了解的朋友请自行略过. 跨域配置:NewGet安装dll Microsofg.AspNet.Cors 然后在App_Start 文件夹下的WebApiConfig.cs中写入跨域配置代码. 1 public static class WebApiConfig 2 { 3 public sta

django之创建第10-1个项目-图片上传并记录上传时间

1.百度云盘:django之创建第10-1个项目-图片上传并记录上传时间 2.主要修改的配置文件有3个,forms.views和models3个文件以及html 3.forms.py文件修改 #coding:utf-8 #这里定义html页面中用到的表单,常和views和models文件配合使用 """ >>> help(django) Help on package django: PACKAGE CONTENTS conf (package) contr