本文主要和大家分享layui上传组件使用方法,先贴上前端的代码,希望能帮助到大家。
产品特性Form @*table { height: 150px; } .layui-form-label { width: 100px; }*@
function getModelName() { var url = location.search; //获取url中"?"符后的字串 var theRequest = new Object(); if (url.indexOf("?") != -1) { var str = url.substr(1); strs = str.split("&"); for (var i = 0; i < strs.length; i++) { theRequest[strs[i].split("=")[0]] = unescape(strs[i].split("=")[1]); } } return theRequest; }; var parentUrlObj = getModelName(); var FileType = decodeURI(escape(parentUrlObj['filetype'])); var ModelId = decodeURI(escape(parentUrlObj['modelId'])); var NodeId = decodeURI(escape(parentUrlObj['nodeid'])); var ProductId = decodeURI(escape(parentUrlObj['productid'])); layui.use(['form', 'upload'], function () { var form = layui.form, upload = layui.upload; var demoListView = $('#demoList') , uploadListIns = upload.render({ elem: '#testList' , url: '/ModelList/uploadNodeAttributeFile?filetype=' + FileType + '&modelid=' + ModelId + '&nodeid=' + NodeId + '&productid=' + ProductId , accept: 'file' , multiple: true , auto: false , bindAction: '#testListAction' , 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) { if (res.Status == "success") { var tr = demoListView.find('tr#upload-' + index), tds = tr.children(); tds.eq(2).html('
上传成功'); return null; } else { if (res.Message == "文件已存在") { var tr = demoListView.find('tr#upload-' + index), tds = tr.children(); tds.eq(2).html('
上传失败,文件已存在'); return null; } else { this.error(index, upload); } } } , 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'); //显示重传 } }); }); function heightTiao(nameid) { var oIframe = window.top.document.getElementById(nameid); var oBody = document.getElementsByTagName("body")[0]; oIframe.style.height = oBody.offsetHeight + 40 + 'px'; };
登录后复制
C#后端接收代码
HttpFileCollection hfc = System.Web.HttpContext.Current.Request.Files;string str1 = AppDomain.CurrentDomain.SetupInformation.ApplicationBase;string imgPath = "";string fileName = "";fileName = hfc[0].FileName;imgPath = Server.MapPath("~/bin/" + fileName);imgPath = Server.MapPath("~/bin/" + modelId + "/" + productid + "/" + nodeid + "/" + filetype + "/" + fileName);hfc[0].SaveAs(imgPath);return Content(new AjaxResult { Status = ResultType.success, Message = "执行成功" }.ToJson());
登录后复制
具体使用过程中需要注意的是
auto: false
bindAction: ‘#testListAction’
这两个参数主要是设定为当你选择文件时不上传文件,而指定某个按钮来执行上传的动作
假设需要选择文件时则执行上传动作,只需要把auto设为true,并且去掉bindAction这个参数
其它参数可参照layui官网文档
上传文件需要判断的很多,只是简单记录个例子方便自己以后的使用。
相关推荐:
layui.js表单验证功能实例分享
layui.js表单验证功能实例分享
layui.js表单验证功能实例分享
以上就是layui上传组件使用方法分享的详细内容,更多请关注【创想鸟】其它相关文章!
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至253000106@qq.com举报,一经查实,本站将立刻删除。
发布者:PHP中文网,转转请注明出处:https://www.chuangxiangniao.com/p/2785117.html