本文实例讲述了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.eoogi.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
华山资源网 Design By www.eoogi.com
暂无评论...
RTX 5090要首发 性能要翻倍!三星展示GDDR7显存
三星在GTC上展示了专为下一代游戏GPU设计的GDDR7内存。
首次推出的GDDR7内存模块密度为16GB,每个模块容量为2GB。其速度预设为32 Gbps(PAM3),但也可以降至28 Gbps,以提高产量和初始阶段的整体性能和成本效益。
据三星表示,GDDR7内存的能效将提高20%,同时工作电压仅为1.1V,低于标准的1.2V。通过采用更新的封装材料和优化的电路设计,使得在高速运行时的发热量降低,GDDR7的热阻比GDDR6降低了70%。
更新日志
2024年11月16日
2024年11月16日
- 第五街的士高《印度激情版》3CD [WAV+CUE][2.4G]
- 三国志8重制版哪个武将智力高 三国志8重制版智力武将排行一览
- 三国志8重制版哪个武将好 三国志8重制版武将排行一览
- 三国志8重制版武将图像怎么保存 三国志8重制版武将图像设置方法
- 何方.1990-我不是那种人【林杰唱片】【WAV+CUE】
- 张惠妹.1999-妹力新世纪2CD【丰华】【WAV+CUE】
- 邓丽欣.2006-FANTASY【金牌大风】【WAV+CUE】
- 饭制《黑神话》蜘蛛四妹手办
- 《燕云十六声》回应跑路:年内公测版本完成95%
- 网友发现国内版《双城之战》第二季有删减:亲亲环节没了!
- 邓丽君2024-《漫步人生路》头版限量编号MQA-UHQCD[WAV+CUE]
- SergeProkofievplaysProkofiev[Dutton][FLAC+CUE]
- 永恒英文金曲精选4《TheBestOfEverlastingFavouritesVol.4》[WAV+CUE]
- 群星《国风超有戏 第9期》[320K/MP3][13.63MB]
- 群星《国风超有戏 第9期》[FLAC/分轨][72.56MB]