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

您所在的位置:网站首页 layui上传图片demo Layui实现文件或图片上传记录

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

2022-06-01 17:01| 来源: 网络整理| 查看: 265

本文为大家分享了layui实现文件或图片上传记录的具体代码,供大家参考,具体内容如下

首先是layui自己的官网关于图片/文件上传的帮助文档:

接下来是我自己的使用记录:

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 = $(['' ,''+ file.name +'' ,''+ (file.size/1014).toFixed(1) +'kb' ,'等待上传' ,'' ,'重传' ,'删除' ,'' ,''].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('上传成功'); tds.eq(3).html('查看'); //清空操作 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('上传失败'); tds.eq(3).find('.demo-reload').removeClass('layui-hide'); //显示重传 } }); $(".layui-upload-file").hide(); });

上述js代码中出现的相关html元素如下,相关引入js文件和css为:bootstrap3的js和css及layui的js文件即可

× 上传检验报告 选择多文件 开始上传 文件名 大小 状态 操作 关闭

3.在打开模态框时可以对1中定义的变量进行动态赋值,这些变量会相应的传到后台中:

function showUploadModal(id) { //动态赋值 uploadListIns.config.data.tableRecordId = id; uploadListIns.config.data.filenamePrefix = id+".自检pass."; $("#uploadModal").modal("show"); }

4.最终前端实现效果如下:

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持前端开发者。

» 本文来自:前端开发者 » 《Layui实现文件或图片上传记录_白泊_前端开发者》 » 本文链接地址:https://www.rokub.com/64060.html » 您也可以订阅本站:https://www.rokub.com


【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3