利用layui前端框架實現對不同文件夾的多文件上傳

利用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':# 上傳文件的鍵名是fileif '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.filenamelogging.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)passelse: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'})

下面簡單展示一下效果圖:
1323525-20190329145904852-362584434.png

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

轉載于:https://www.cnblogs.com/We612/p/10621230.html

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/387125.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/387125.shtml
英文地址,請注明出處:http://en.pswp.cn/news/387125.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

ps、grep和kill聯合使用殺掉進程

ps、grep和kill聯合使用殺掉進程例如要殺掉hello這個進程&#xff0c;使用下面這個命令就能直接實現。ps -ef |grep hello |awk {print $2}|xargs kill -9這里是輸出ps -ef |grep hello 結果的第二列的內容然后通過xargs傳遞給kill -9,其實第二列內容就是hello的進程號&#xf…

03 控制語句

if語句 if age > 18 print your age is, age else print teenager Python代碼的縮進規則&#xff1a;具有相同縮進的代碼被視為代碼塊。 if age > 18 print adult elif age > 6 print teenager elif age > 3 print kid else print baby for循環 L [Adam, L…

yum 來安裝 nodejs

要通過 yum 來安裝 nodejs 和 npm 需要先給 yum 添加 epel 源&#xff0c;添加方法在 centos 添加epel和remi源 中##添加 epel 源 64位: rpm -ivh http://download.fedoraproject.org/pub/epel/6/x86_64/epel-release-6-8.noarch.rpm32位: rpm -ivh http://download.fedoraproj…

yzh的神仙題

U66905 zz題 考慮一個點權值被計算了多少次。。。不知 所以對未來承諾&#xff0c;方便直接算上總數&#xff01; 然后其實是給邊定向&#xff0c;即先刪除fa和son的哪一個 f[x][j]&#xff0c;會計算j次 無法轉移 f[x][j][k]&#xff0c;其中會從子樹計算k次。 當邊從兒子指向…

04 函數

內置函數 Python內置了很多有用的函數&#xff0c;可以直接調用。 要調用一個函數&#xff0c;需要知道函數的名稱和參數。 可以直接從Python的官方網站查看文檔&#xff1a;http://docs.python.org/2/library >>> abs(-20) >>> help(abs) >>>…

iview render的時候可以寫控件的基本格式

render: (h, params) > {return h(div, [h(Button, {props: {type: id,size: small},style: {marginRight: 5px},on: {click: () > {this.pojectshow(this.datatable[params.index].id)}}}, 詳情),h(Button, {props: {type: id,size: small},style: {marginRight: 5px},o…

ES6基本使用

var let 度可用于聲明變量. 區別&#xff1a;1、let&#xff1a;只在let命令所在代碼塊內有效 2、let 不存在變量提升&#xff08;內部影響不到外部&#xff09; var b [];for(var j0;j<10;j){let dj;b[j]function(){console.log(d);};}b[3]() //3 3、let 不允許在相同作用…

Axios的Vue插件(添加全局請求/響應攔截器)

/*** file Axios的Vue插件&#xff08;添加全局請求/響應攔截器&#xff09;*/// https://github.com/mzabriskie/axios import axios from axios// 攔截request,設置全局請求為ajax請求 axios.interceptors.request.use((config) > {config.headers[X-Requested-With] XML…

05 切片、迭代、列表生成

切片 >>> L [Adam, Lisa, Bart, Paul] >>> L[0:3] #取前3個元素 >>> L[:3] >>> L[1:3] >>> L[:] >>> L[::2] #第三個參數表示每2個元素取一個元素&#xff0c;也就是隔一個取一個 [Adam,Bart] >>>…

一個例子徹底搞懂C++的虛函數和純虛函數

學習C的多態性&#xff0c;你必然聽過虛函數的概念&#xff0c;你必然知道有關她的種種語法&#xff0c;但你未必了解她為什么要那樣做&#xff0c;未必了解她種種行為背后的所思所想。深知你不想在流于表面語法上的蜻蜓點水似是而非&#xff0c;今天我們就一起來揭開擋在你和虛…

利用Caffe實現mnist的數據訓練

阿里云的參考文檔&#xff1a;https://help.aliyun.com/document_detail/49571.html在文檔里提供了caffe的一個案例&#xff0c;利用Caffe實現mnist的數據訓練。準備的數據源可以在“深度學習案例代碼及數據下載”頁找到Caffe數據下載并解壓。要訓練自己的圖片&#xff0c;還是…

06 函數式編程

1 函數式編程簡介 函數&#xff1a;function 函數式&#xff1a;functional 一種編程范式 特點&#xff1a; 把計算視為函數而非指令 純函數式編程&#xff1a;不需要變量&#xff0c;沒有副作用&#xff0c;測試簡單 支持高階函數&#xff0c;代碼簡潔 Python支持的函數式…

Android SDK開發

目前我們的應用內使用了 ArcFace 的人臉檢測功能&#xff0c;其他的我們并不了解&#xff0c;所以這里就和大家分享一下我們的集成過程和一些使用心得 集成 ArcFace FD 的集成過程非常簡單 在 ArcFace FD 的文檔上有說明支持的系統為 5.0 及以上系統&#xff0c;但其實在 4.4 系…

jQuery WeUI 上傳

jQuery WeUI 是專為微信公眾賬號開發而設計的一個框架&#xff0c;jQuery WeUI的官網&#xff1a;http://jqweui.com/ 需求&#xff1a;需要在微信公眾號網頁添加上傳圖片功能 技術選型&#xff1a;實現上傳圖片功能可選百度的WebUploader、餓了么的Element和微信的jQuery WeUI…

07 模塊

模塊和包的概念 等同于java中的Package 模塊名文件名&#xff08;無后綴&#xff09; 在文件系統中&#xff0c;包就是文件夾&#xff0c;模塊就是xxx.py文件 每層包下面都有__init__.py文件 導入模塊 >>> import math >>> math.pow(2, 0.5) >>…

1.rabbitmq 集群版安裝及使用nginx進行四層負載均衡設置

1.安裝erlang 需要注意erlang的版本是否滿足rabbitmq的需求 這里用到的版本是&#xff1a;Erlang 19.0.4 RabbitMQ 3.6.15 wget http://www.rabbitmq.com/releases/erlang/erlang-19.0.4-1.el7.centos.x86_64.rpmrpm -ivh erlang-19.0.4-1.el7.centos.x86_64.rpm yum -y inst…

使用WEUI uploader上傳圖片

使用WEUI uploader上傳圖片&#xff0c;博主費了很大的勁總算找到完整的了&#xff0c;并且帶后臺接收代碼&#xff0c;有需要的朋友拿去吧&#xff0c;親測可用&#xff01; 一、html代碼<link rel"stylesheet" href"https://res.wx.qq.com/open/libs/weui/…

08 面向對象編程

1 介紹 面向對象編程是一種程序設計范式 把程序看做不同對象的相互調用&#xff0c;對現實世界建立對象模型。 面向對象編程的基本思想&#xff1a; 類和實例&#xff1a; 類用于定義抽象類型 實例根據類的定義被創建出來 2 定義類并創建實例 類通過class關鍵字定義&…

H5+jqweui實現手機端圖片壓縮上傳 Base64

H5jqweui實現手機端圖片壓縮上傳主要功能&#xff0c;使用H5的formData上傳base64格式的圖片&#xff0c;canvas壓縮圖片&#xff0c;前端樣式使用weui&#xff0c;為方便起見&#xff0c;使用了jquery封裝過的weui&#xff0c;jqweui。話不多少&#xff0c;開始上代碼。前端代…

09 類的繼承

繼承一個類 class Person(object): def __init__(self, name, gender): self.name name self.gender gender class Student(Person): def __init__(self, name, gender, score): super(Student, self).__init__(name, gender) self.score score 判斷類型 isinstance()可以…