layui如何做图片上传
layui上传功能的实现:
一、前往官网下载layui的框架
打开html文件,引用其中的 layui.css 和 layui.js
二、调用前端的html代码,设置id值。
<div class="layui-upload upload">
<button type="button" id="upload">上传图片</button>
<input type="file" accept="" name="file">
<div class="layui-upload-list">
<img id="demo1">
<p id="demoText"></p>
</div>
</div>
<script>
layui.use(['laypage', 'layer', 'upload'], function () {
var laypage = layui.laypage //设置配置环境
, layer = layui.layer
, upload = layui.upload
//上传图片
var uploadInst = upload.render({
elem: '#upload'
, url: '/upload/' //改成您自己的上传接口
, before: function (obj) {
//预读本地文件示例,不支持ie8
obj.preview(function (index, file, result) {
$('#demo1').attr('src', result); //图片链接(base64)
});
}
, done: function (res) {
//如果上传失败
if (res.code > 0) {
return layer.msg('上传失败');
}
//上传成功
}
, error: function () {
//演示失败状态,并实现重传
var demoText = $('#demoText');
demoText.html('<span style="color: #FF5722;">上传失败</span>
<a class="layui-btn layui-btn-xs demo-reload">重试</a>');
demoText.find('.demo-reload').on('click', function () {
uploadInst.upload();
});
}
});
});
</script>
本文出至:学新通
- 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
- 本站站名: 学新通
- 本文地址: https://www.swvq.com/boutique/detail/1899
- 联系方式: luke.wu@swvq.com
- 来源链接: www.php.cn/layui/465477.html
系列文章
更多
同类精品
更多
精彩评论
-
windows上查看nginx是否启动
PHP中文网 04-19 -
2023年最新的28道PHP面试题附答案
PHP中文网 03-27 -
docker hub 进不去怎么办
PHP中文网 03-15 -
推荐五款xml编辑工具
PHP中文网 03-04 -
navicat怎样清除注册表
PHP中文网 04-05 -
ChatGPT应用通过Axios+EventSource使用GPT3.5 API
uWydnA 03-13 -
强力推荐10款好看使用的Bootstrap后台管理系统模板
PHP中文网 03-09 -
vscode怎么调整代码大小两种方法
PHP中文网 03-11 -
navicat删除的数据能还原吗
PHP中文网 04-09 -
navicat连接sqlserver数据库
PHP中文网 04-03