最新文章专题视频专题问答1问答10问答100问答1000问答2000关键字专题1关键字专题50关键字专题500关键字专题1500TAG最新视频文章推荐1 推荐3 推荐5 推荐7 推荐9 推荐11 推荐13 推荐15 推荐17 推荐19 推荐21 推荐23 推荐25 推荐27 推荐29 推荐31 推荐33 推荐35 推荐37视频文章20视频文章30视频文章40视频文章50视频文章60 视频文章70视频文章80视频文章90视频文章100视频文章120视频文章140 视频2关键字专题关键字专题tag2tag3文章专题文章专题2文章索引1文章索引2文章索引3文章索引4文章索引5123456789101112131415文章专题3
当前位置: 首页 - 科技 - 知识百科 - 正文

layui上传组件使用方法分享

来源:动视网 责编:小采 时间:2020-11-27 20:01:55
文档

layui上传组件使用方法分享

layui上传组件使用方法分享:本文主要和大家分享layui上传组件使用方法,先贴上前端的代码,希望能帮助到大家。<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-
推荐度:
导读layui上传组件使用方法分享:本文主要和大家分享layui上传组件使用方法,先贴上前端的代码,希望能帮助到大家。<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-


本文主要和大家分享layui上传组件使用方法,先贴上前端的代码,希望能帮助到大家。

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head>
 <title>产品特性Form</title>
 <link href="~/Content/Base/layui/css/layui.css" rel="stylesheet" type="text/css" />
 <script src="~/Content/Base/layui/layui.js" type="text/javascript"></script>
 <script src="../../../../Content/Views/js/jquery-1.8.3.min.js" type="text/javascript"></script>
 <script src="../../../../Content/Views/js/framework-ui.js" type="text/javascript"></script>
 <style>
 @*table
 { height: 150px;
 } .layui-form-label
 { width: 100px; }*@ </style></head><body>


 <p style="width:100%;">
 <p class="layui-upload">
 <button type="button" class="layui-btn layui-btn-normal" id="testList">选择多文件</button> 
 <p class="layui-upload-list">
 <table class="layui-table" id="tableFile">
 <thead>
 <tr><th>文件名</th>
 <th>大小</th>
 <th>状态</th>
 <th>操作</th>
 </tr></thead>
 <tbody id="demoList"></tbody>
 </table>
 </p>
 <button type="button" class="layui-btn" id="testListAction">开始上传</button>
 </p> 
 </p>


 <script>

 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 = $(['<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-mini demo-reload layui-hide">重传</button>'
 , '<button class="layui-btn layui-btn-mini 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) {
 if (res.Status == "success") { var tr = demoListView.find('tr#upload-' + index), tds = tr.children();
 tds.eq(2).html('<span style="color: #5FB878;">上传成功</span>'); return null;
 } else { if (res.Message == "文件已存在") { var tr = demoListView.find('tr#upload-' + index), tds = tr.children();
 tds.eq(2).html('<span style="color: #5FB878;">上传失败,文件已存在</span>'); 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('<span style="color: #FF5722;">上传失败</span>');
 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';
 }; </script></body></html>

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上传组件使用方法分享

layui上传组件使用方法分享:本文主要和大家分享layui上传组件使用方法,先贴上前端的代码,希望能帮助到大家。<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-
推荐度:
标签: 分享 上传 使用
  • 热门焦点

最新推荐

猜你喜欢

热门推荐

专题
Top