利用layui前端框架实现对不同文件夹的多文件上传

问题场景:

普通的input标签实现多文件上传时,只能对同一个文件夹下的多个文件进行上传,如果要同时上传两个或多个文件夹下的文件,是无法实现的。这篇文章就是利用layui中的插件,解决这个问题。

普通多文件上传标签:

前端 运用layui

操作步骤:

1、进入layui首页,下载整个组件

2、下载完成后,把名字为layui的文件夹放到你的项目中进行引用

3、引用layui.js和layui.css实现功能

4、可点击可进入layui文件上传实例的官方网址进行参考,来以上三步的前端代码实现

HTML代码块:

<div class="layui-upload">
<button type="button" class="layui-btn layui-btn-normal"
style="margin-left: 30px"
id="testList">选择多文件
</button>
<button type="button" class="layui-btn" id="testListAction"
style="display: inline; margin-left: 26px;">
开始上传
</button>
<div class="layui-upload-list col-md-12">
<table class="layui-table" style="margin: 0 0 0 0">
<thead style="display: none">
<tr>
<th>文件名</th>
<th>大小</th>
<th>状态</th>
<th>操作</th>
</tr>
</thead>
<tbody id="demoList"></tbody>
</table>
</div>
</div>

JS代码块


<script>
layui.use('upload', function () {
var $ = layui.jquery
, upload = layui.upload;
//多文件列表示例
var demoListView = $('#demoList')
, uploadListIns = upload.render({
elem: '#testList'
, url: '/task_mgm/taskinfo_upload'
, 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-xs demo-reload layui-hide">重传</button>'
, '<button class="layui-btn layui-btn-xs 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.code == 0) { //上传成功
var tr = demoListView.find('tr#upload-' + index)
, tds = tr.children();
tds.eq(2).html('<span style="color: #5FB878;">上传成功</span>');
{#tds.eq(3).html(''); //清空操作#}
return delete this.files[index]; //删除文件队列已经上传成功的文件
}
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'); //显示重传
}
});
})
</script>

Python后端 代码块

UPLOAD_FOLDER = 'static_files/task_mgm/'
# 设置允许上传的文件类型
ALLOWED_EXTENSIONS = set(
['txt', 'png', 'jpg', 'xls', 'JPG', 'PNG', 'xlsx', 'gif', 'GIF', 'ppt', 'pptx', 'doc', 'docx', 'csv', 'sql', 'py',
'rar']) # 用于判断文件后缀
def allowed_file(filename):
return '.' in filename and filename.rsplit('.', 1)[1] in ALLOWED_EXTENSIONS @task_mgm.route('/taskinfo_upload',method=['post'])
@login_required
def taskINfo_upload_fun():
if request.method == 'POST':
# 上传文件的键名是file
if 'file' not in request.files:
logging.debugp('No file part')
return jsonify({'code': -1, 'filename':'', 'msg':'No file part'})
# 获取文件对象
file = request.files['file']
# 若用户没有选择文件就提交,提示‘No selected file’
if file.filename == '':
logging.debug('No selected file')
return jsonify({'code': -1', 'filename':'', 'msg':'No selected file'})
else:
try:
if file and allowed_file(file.filename):
origin_file_name = file.filename
logging.debug('filename is %s' % origin_file_name)
file_dir = os.path.join(os.getcwd(), UPLOAD_FOLDER)
if os.path.exists(file_dir):
logging.debug('%s path exist' % file_dir)
pass
else:
logging.debug('%s path not exist' % file_dir)
os.makedirs(file_dir)
file.save(os.path.join(file_dir, filename))
return jsonify({'code':0, 'filename':origin_file_name, 'msg': 'save successfully'})
else:
logging.debug('%s not allowed' % file.filename)
return jsonify({'code':-1, 'filename':'', 'msg': 'File not allowed'})
except Exception as e:
logging.debug(e)
return jsonify({'code':-1, 'filename':'', 'msg':'Error occurred'})
else:
return jsonify({'code':-1, 'filename': '', 'msg':'Method not allowed'})

下面简单展示一下效果图:

到此为止,前后端代码都有了,可以粘去直接使用!!!!

利用layui前端框架实现对不同文件夹的多文件上传的更多相关文章

  1. JFinal中文件上传后会默认放置到WebContent的upload包下,但是tomcat会自动重启,当我们再次打开upload文件夹查看我们刚刚上传的文件时,发现上传的文件已经没有了。

    JFinal中文件上传后会默认放置到WebContent的upload包下,但是tomcat会自动重启,当我们再次打开upload文件夹查看我们刚刚上传的文件时,发现上传的文件已经没有了.因为tomc ...

  2. input文件上传(上传单个文件/多选文件/文件夹、拖拽上传、分片上传)

    //上传单个/多个文件 <input title="点击选择文件" id="h5Input1" multiple="" accept= ...

  3. pure-ftpd管理FTP服务器,创建文件夹可以,但上传下载文件不行

    两种原因 1.因为pure-ftpd的防火墙端口问题 # Port range for passive connections replies. - for firewalling. PassiveP ...

  4. layui前端框架

    项目中需要弹出层效果,使用了layui前端框架,主要使用了里面的弹出层特效(可以移动) html代码 要给这个标签绑定click方法 <a href='javascript:;' data-me ...

  5. layui前端框架实例(修复官网数据接口异常问题)

    layui前端框架实例,官网的实例会提示数据接口异常,已修复. 主要是修改数据表格,做一个可以用的实例,可以选中,编辑,删除等. gitee地址:https://gitee.com/pingg2019 ...

  6. CI框架如何在主目录application目录之外使用uploadify上传插件和bootstrap前端框架:

    19:29 2016/3/10CI框架如何在主目录application目录之外使用uploadify上传插件和bootstrap前端框架:项目主路径:F:\wamp\www\graduationPr ...

  7. 利用shell脚本或者php移动某个文件夹下的文件到各自的日期组成的目录下

    背景是这样的:网站一开始访问量比较小,大家就把所有的图片文件上传到一个目录下(比如是/data/images/).后来访问量大了,图片也多了,这样就影响读取效率.所以有个这样的需求,把这些个图片文件移 ...

  8. 利用nodejs监控文件变化并使用sftp上传到服务器

    很久没写博客了,因为最近在用react+express做一个自己的工具型网站(其实就是夺宝岛抢拍器) 然后因为经常要改动,而且又要放到服务器上进行测试.总是要webpack,然后手动把文件上传上去,不 ...

  9. 前端通信:ajax设计方案(三)--- 集成ajax上传技术

    在此之前让我感慨一下现在的前端开发的氛围.我遇到好多人,给我的观念都是,这个东西这个框架有了,那个东西那个框架做了,前端嘛,学几个框架,这个拼凑一下那个拼凑一下就好了.其实我想问,东西都框架做了,那你 ...

随机推荐

  1. springboot项目容器化

    创建一个简单的springboot项目,依赖中加入: 编写一个Restfull接口: 编写启动类: 启动项目,浏览器访问该接口,得到想要的结果.下面,就将这个项目进行Docker容器化(applica ...

  2. k8s健康检查(七)--技术流ken

    默认的健康检查 强大的自愈能力是 Kubernetes 这类容器编排引擎的一个重要特性.自愈的默认实现方式是自动重启发生故障的容器.除此之外,用户还可以利用 Liveness 和 Readiness ...

  3. celery异步消息处理框架

    Celery 1.什么是Clelery Celery是一个简单.灵活且可靠的,处理大量消息的分布式系统 专注于实时处理的异步任务队列 同时也支持任务调度 Celery架构 Celery的架构由三部分组 ...

  4. Math.floor(Math.random()*3+1)

    Math.random():获取0~1随机数 Math.floor() method rounds a number DOWNWARDS to the nearest integer, and ret ...

  5. Jinja2用法总结

    Jinja2用法总结   一:渲染模版 要渲染一个模板,通过render_template方法即可. @app.route('/about/') def about(): # return rende ...

  6. wordpress的excerpt()函数

    问题:在wordpres中的single页面,本身引用的<?php the_excerpt(); ?>,但是在页面上显示的却是文章的内容 原因:the_excerpt(); 在excerp ...

  7. Python 基于pykafka简单实现KAFKA消费者

    基于pykafka简单实现KAFKA消费者   By: 授客 QQ:1033553122         1.测试环境 python 3.4 zookeeper-3.4.13.tar.gz 下载地址1 ...

  8. Python使用Plotly绘图工具,绘制甘特图

    今天来讲一下如何使用Python 的绘图工具Plotly来绘制甘特图的方法 甘特图大家应该了解熟悉,就是通过条形来显示项目的进度.时间安排等相关情况的. 我们今天来学习一下,如何使用ployly来绘制 ...

  9. 轻松学习UML之用例图,时序图

    本文主要讲解UML图中的用例图(Use Case Diagram)和时序图(Sequence Diagram)相关内容,如有不足之处,还请指正. 概述 统一建模语言(UML,UnifiedModeli ...

  10. Git 简单粗暴使用

    1.现在总结一下今天学的两点内容: 初始化一个Git仓库,使用git init命令. 添加文件到Git仓库,分两步: 第一步,使用命令git add <file>,注意,可反复多次使用,添 ...