Share how to use layui upload component
This article mainly shares with you how to use the layui upload component. First, paste the front-end code. I hope it can help everyone.
<!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#Backend receiving code
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());
What you need to pay attention to during specific use is
auto: false
bindAction: '#testListAction'
These two This parameter is mainly set to not upload files when you select files, but to specify a button to perform the upload action.
Assuming that you need to perform the upload action when you select a file, you only need to set auto to true and remove bindAction. This parameter
Other parameters can refer to the layui official website documentation
There are many things that need to be judged when uploading files. I just record an example to facilitate your future use.
Related recommendations:
layui.js form verification function example sharing
detailed explanation of vue Alibaba Cloud upload component
Introduction to common methods of jQuery layui
The above is the detailed content of Share how to use layui upload component. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

1. First, we enter NetEase Cloud Music, and then click on the software homepage interface to enter the song playback interface. 2. Then in the song playback interface, find the sharing function button in the upper right corner, as shown in the red box in the figure below, click to select the sharing channel; in the sharing channel, click the "Share to" option at the bottom, and then select the first "WeChat Moments" allows you to share content to WeChat Moments.

layui provides a variety of methods for obtaining form data, including directly obtaining all field data of the form, obtaining the value of a single form element, using the formAPI.getVal() method to obtain the specified field value, serializing the form data and using it as an AJAX request parameter, and listening Form submission event gets data.

Recently, Baidu Netdisk Android client has ushered in a new version 8.0.0. This version not only brings many changes, but also adds many practical functions. Among them, the most eye-catching is the enhancement of the folder sharing function. Now, users can easily invite friends to join and share important files in work and life, achieving more convenient collaboration and sharing. So how do you share the files you need to share with your friends? Below, the editor of this site will give you a detailed introduction. I hope it can help you! 1) Open Baidu Cloud APP, first click to select the relevant folder on the homepage, and then click the [...] icon in the upper right corner of the interface; (as shown below) 2) Then click [+] in the "Shared Members" column 】, and finally check all

Layui login page jump setting steps: Add jump code: Add judgment in the login form submit button click event, and jump to the specified page through window.location.href after successful login. Modify the form configuration: add a hidden input field to the form element of lay-filter="login", with the name "redirect" and the value being the target page address.

Adaptive layout can be achieved by using the responsive layout function of the layui framework. The steps include: referencing the layui framework. Define an adaptive layout container and set the layui-container class. Use responsive breakpoints (xs/sm/md/lg) to hide elements under specific breakpoints. Specify element width using the grid system (layui-col-). Create spacing via offset (layui-offset-). Use responsive utilities (layui-invisible/show/block/inline) to control the visibility of elements and how they appear.

The difference between layui and Vue is mainly reflected in functions and concerns. Layui focuses on rapid development of UI elements and provides prefabricated components to simplify page construction; Vue is a full-stack framework that focuses on data binding, component development and state management, and is more suitable for building complex applications. Layui is easy to learn and suitable for quickly building pages; Vue has a steep learning curve but helps build scalable and easy-to-maintain applications. Depending on the project needs and developer skill level, the appropriate framework can be selected.

To run layui, perform the following steps: 1. Import layui script; 2. Initialize layui; 3. Use layui components; 4. Import layui styles (optional); 5. Ensure script compatibility and pay attention to other considerations. With these steps, you can build web applications using the power of layui.

The layui framework is a JavaScript-based front-end framework that provides a set of easy-to-use UI components and tools to help developers quickly build responsive web applications. Its features include: modular, lightweight, responsive, and has complete documentation and community support. layui is widely used in the development of management backend systems, e-commerce websites, and mobile applications. The advantages are quick start-up, improved efficiency, and easy maintenance. The disadvantages are poor customization and slow technology updates.
