日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

Python文件上传功能简单实现

發布時間:2023/12/9 python 27 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Python文件上传功能简单实现 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

本文章代碼上傳在碼云上

代碼地址

git@gitee.com:DanYuJie/upanddown.git

這里我們使用flask框架,簡單實用

目錄結構: upandown/static/css/js/jquery.min.jstoastr.min.jstemplates/index.htmltest.py

首先我們需要一個頁面在templates/index.html(這里使用form表單實現)

<!DOCTYPE html> <html lang="en"> <head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><meta http-equiv="X-UA-Compatible" content="ie=edge"><link rel="stylesheet" href="../static/css/toastr.min.css"><script src="../static/js/jquery.min.js"></script><script src="../static/js/toastr.min.js"></script><title>Document</title> </head> <body><form method="POST" action="/upload" enctype="multipart/form-data"><input type="file" name="file" id="file"><input type="submit" value="upload"><a href=""></a></form><hr><ol id="filelist"></ol><script>function checkstatus(){if('{{status}}'== 'OK'){toastr['success']("上傳成功");}else if('{{status}}'== 'null'){toastr['error']("上傳失敗"); }}function get_list(){$.ajax({url:'/getlist',type:'GET',success:function(result){len_result = result.length;for(var x =0; x < len_result; x++){$("#filelist").append('<br><a href=/download/' + result[x] + '>' + result[x] +'</a>');}alert(content_list);},error:function(){alert("失敗");}});}checkstatus();get_list();</script> </body> </html>

然后是后臺接收

test.py

#!/usr/bin/env python # -*- coding:utf-8 -*- from flask import Flask,render_template, request, send_from_directory,jsonify, redirect import os # import sys # reload(sys) # sys.setdefaultencoding('utf-8') app = Flask(__name__)# ALLOWED_EXTENSTIONS = set(['png', 'jpg', 'jpeg', 'gif']) app.config['UPLOAD_FOLDER'] = os.getcwd() download_floder = app.config['UPLOAD_FOLDER'] + '/upload'def allow_file(filename):allow_list = ['png', 'PNG', 'jpg', 'doc', 'docx', 'txt', 'pdf', 'PDF', 'xls', 'rar', 'exe', 'md', 'zip'] a = filename.split('.')[1]if a in allow_list:return Trueelse:return False@app.route('/main') def home():return render_template('index.html')@app.route('/getlist') def getlist():file_url_list = []file_floder = app.config['UPLOAD_FOLDER'] + '/upload'file_list = os.listdir(file_floder)for filename in file_list:file_url = url_for('download',filename=filename)file_url_list.append(file_url)# print file_listreturn jsonify(file_list)@app.route('/download/<filename>') def download(filename):return send_from_directory(download_floder,filename, as_attachment=True)@app.route('/upload', methods=['POST', 'GET']) def upload():file = request.files['file']if not file:return render_template('index.html', status='null')# print type(file)if allow_file(file.filename):file.save(os.path.join(app.config['UPLOAD_FOLDER']+'/upload/', file.filename))return render_template('index.html', status='OK')else:return 'NO'if __name__ == '__main__':app.run(debug=True, host='0.0.0.0')

?



作者:LanceAdd
鏈接:https://www.jianshu.com/p/fbcd1760cae0
來源:簡書
簡書著作權歸作者所有,任何形式的轉載都請聯系作者獲得授權并注明出處。

總結

以上是生活随笔為你收集整理的Python文件上传功能简单实现的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。