Python使用Flask框架同时上传多个文件的方法
作者:niuniu 时间:2023-02-02 10:16:49
本文实例讲述了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程序设计有所帮助。
标签:Python,Flask,框架,上传,文件
0
投稿
猜你喜欢
python2.7安装图文教程
2021-01-02 01:36:15
基于Python实现千图成像工具的示例代码
2022-03-14 12:19:21
vue中的Object.freeze() 优化数据方式
2024-04-10 16:10:25
一篇文章带你了解Python中的类
2022-10-11 19:46:18
使用python批量转换文件编码为UTF-8的实现
2023-03-07 03:19:41
python中set()函数简介及实例解析
2022-05-15 17:12:24
python3+PyQt5实现使用剪贴板做复制与粘帖示例
2023-02-14 05:37:10
SQL 优化
2024-01-16 10:50:40
深入浅出SQL之左连接、右连接和全连接
2009-08-30 15:14:00
已解决卸载pip重新安装的方法
2023-09-27 22:08:02
利用python实现周期财务统计可视化
2022-03-19 21:30:41
删除sqlserver数据库日志和没有日志的数据库恢复办法
2024-01-21 23:20:55
python GUI库图形界面开发之PyQt5图片显示控件QPixmap详细使用方法与实例
2023-05-31 17:41:29
MySQL中索引优化distinct语句及distinct的多字段操作
2024-01-18 20:43:38
Python编程应用设计原则详解
2021-04-08 20:13:05
通过python调用adb命令对App进行性能测试方式
2023-10-05 06:24:25
简单了解Django应用app及分布式路由
2023-08-30 19:29:47
Python threading.local代码实例及原理解析
2021-09-03 06:14:07
php注册系统和使用Xajax即时验证用户名是否被占用
2023-09-12 05:27:55
Windows存储 SQL行溢出 差异备份及疑问
2008-12-24 15:22:00