圆月山庄资源网 Design By www.vgjia.com
本文实例讲述了Python使用Flask框架同时上传多个文件的方法,分享给大家供大家参考。具体如下:
下面的演示代码带有详细的html页面和python代码
import os # We'll render HTML templates and access data sent by POST # using the request object from flask. Redirect and url_for # will be used to redirect the user once the upload is done # and send_from_directory will help us to send/show on the # browser the file that the user just uploaded from flask import Flask, render_template, request, redirect, url_for, send_from_directory from werkzeug import secure_filename # Initialize the Flask application app = Flask(__name__) # This is the path to the upload directory app.config['UPLOAD_FOLDER'] = 'uploads/' # These are the extension that we are accepting to be uploaded app.config['ALLOWED_EXTENSIONS'] = set(['txt', 'pdf', 'png', 'jpg', 'jpeg', 'gif']) # For a given file, return whether it's an allowed type or not def allowed_file(filename): return '.' in filename and filename.rsplit('.', 1)[1] in app.config['ALLOWED_EXTENSIONS'] # This route will show a form to perform an AJAX request # jQuery is loaded to execute the request and update the # value of the operation @app.route('/') def index(): return render_template('index.html') # Route that will process the file upload @app.route('/upload', methods=['POST']) def upload(): # Get the name of the uploaded files uploaded_files = request.files.getlist("file[]") filenames = [] for file in uploaded_files: # Check if the file is one of the allowed types/extensions if file and allowed_file(file.filename): # Make the filename safe, remove unsupported chars filename = secure_filename(file.filename) # Move the file form the temporal folder to the upload # folder we setup file.save(os.path.join(app.config['UPLOAD_FOLDER'],filename)) # Save the filename into a list, we'll use it later filenames.append(filename) # Redirect the user to the uploaded_file route, which # will basicaly show on the browser the uploaded file # Load an html page with a link to each uploaded file return render_template('upload.html', filenames=filenames) # This route is expecting a parameter containing the name # of a file. Then it will locate that file on the upload # directory and show it on the browser, so if the user uploads # an image, that image is going to be show after the upload @app.route('/uploads/<filename>') def uploaded_file(filename): return send_from_directory(app.config['UPLOAD_FOLDER'], filename) if __name__ == '__main__': app.run( host="0.0.0.0", port=int("80"), debug=True )
index.html代码
<!DOCTYPE html> <html lang="en"> <head> <link href="bootstrap/3.0.0/css/bootstrap.min.css" rel="stylesheet"> </head> <body> <div class="container"> <div class="header"> <h3 class="text-muted">How To Upload a File.</h3> </div> <hr/> <div> <form action="upload" method="post" enctype="multipart/form-data"> <input type="file" multiple="" name="file[]" class="span3" /><br/> <input type="submit" value="Upload" class="span2"> </form> </div> </div> </body> </html>
upload.html页面:
<!DOCTYPE html> <html lang="en"> <head> <link href="bootstrap/3.0.0/css/bootstrap.min.css" rel="stylesheet"> </head> <body> <div class="container"> <div class="header"> <h3 class="text-muted">Uploaded files</h3> </div> <hr/> <div> This is a list of the files you just uploaded, click on them to load/download them <ul> {% for file in filenames %} <li><a href="{{url_for('uploaded_file', filename=file)}}">{{file}}</a></li> {% endfor %} </ul> </div> <div class="header"> <h3 class="text-muted">Code to manage a Upload</h3> </div> <hr/> <pre> @app.route('/upload', methods=['POST']) def upload(): # Get the name of the uploaded file #file = request.files['file'] uploaded_files = request.files.getlist("file[]") filenames = [] for file in uploaded_files: # Check if the file is one of the allowed types/extensions if file and allowed_file(file.filename): # Make the filename safe, remove unsupported chars filename = secure_filename(file.filename) # Move the file form the temporal folder to the upload # folder we setup file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename)) filenames.append(filename) # Redirect the user to the uploaded_file route, which # will basicaly show on the browser the uploaded file # Load an html page with a link to each uploaded file return render_template('upload.html', filenames=filenames) </pre> </div> </div> </body> </html>
希望本文所述对大家的Python程序设计有所帮助。
圆月山庄资源网 Design By www.vgjia.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
圆月山庄资源网 Design By www.vgjia.com
暂无评论...
更新日志
2024年11月08日
2024年11月08日
- 雨林唱片《赏》新曲+精选集SACD版[ISO][2.3G]
- 罗大佑与OK男女合唱团.1995-再会吧!素兰【音乐工厂】【WAV+CUE】
- 草蜢.1993-宝贝对不起(国)【宝丽金】【WAV+CUE】
- 杨培安.2009-抒·情(EP)【擎天娱乐】【WAV+CUE】
- 周慧敏《EndlessDream》[WAV+CUE]
- 彭芳《纯色角3》2007[WAV+CUE]
- 江志丰2008-今生为你[豪记][WAV+CUE]
- 罗大佑1994《恋曲2000》音乐工厂[WAV+CUE][1G]
- 群星《一首歌一个故事》赵英俊某些作品重唱企划[FLAC分轨][1G]
- 群星《网易云英文歌曲播放量TOP100》[MP3][1G]
- 方大同.2024-梦想家TheDreamer【赋音乐】【FLAC分轨】
- 李慧珍.2007-爱死了【华谊兄弟】【WAV+CUE】
- 王大文.2019-国际太空站【环球】【FLAC分轨】
- 群星《2022超好听的十倍音质网络歌曲(163)》U盘音乐[WAV分轨][1.1G]
- 童丽《啼笑姻缘》头版限量编号24K金碟[低速原抓WAV+CUE][1.1G]