Python使用Flask框架同时上传多个文件的方法
作者:niuniu 发布时间:2023-02-02 10:16:49
标签:Python,Flask,框架,上传,文件
本文实例讲述了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程序设计有所帮助。
0
投稿
猜你喜欢
- 自己从工艺品设计到平面设计到网络设计,虽然设计原则不离其宗,但经验下来的心得告诉自己,设计媒介的变化带来很多媒介自身的特殊性,下面总结下网站
- 虽然现在有许多网页制作工具能让您轻松地完成工作,但如果使用HTML则可以得到更大控制权,下面介绍几个小技巧。1.使用语句来控制文字排版比用好
- IE8 的测试版发布,ACID 3 测试正式推出,听上去是让人兴奋的技术进步,而对中文互联网站却是极大的威胁:这意味着,超过半数的中文网页在
- 下面通过三种方法给大家介绍Pycharm2020.1安装中文语言插件的方法,大家可以参考下:方法一(在搜索不到插件):1.安装好Pychar
- python中通过引用计数来回收垃圾对象,在某些环形数据结构(树,图……),存在对象间的循环引用,比如树的父节点引用子节点,子节点同时引用父
- 流式布局流式布局,也叫做瀑布流布局,是网页中经常使用的一种页面布局方式,它的原理就是将高度固定,然后图片的宽度自适应,这样加载出来的图片看起
- 一:操作session1:session配置Session 的配置文件存储在config/session.php中,配置参数有:(1):配置
- folder.htm<html><head><title>闪亮日子之在线创建文件夹</title&
- 如何用表单的方式推送请求的信息?具体见下:<html><head><title>答复用户信息
- 相机固定不动,通过标定版改动不同方位的位姿进行抓拍import cv2camera=cv2.VideoCapture(1)i = 0whil
- 本文实例讲述了Python从序列中移除重复项且保持元素间顺序不变的方法。分享给大家供大家参考,具体如下:问题:从序列中移除重复的元素,但仍然
- 一、网络请求在uni中可以调用uni.request方法进行请求网络请求需要注意的是:在小程序中网络相关的 API 在使用前需要配置域名白名
- (1)抓取今日头条街拍图片(2)分析今日头条街拍图片结构keyword: 街拍pd: atlasdvpf: pcaid: 4916page_
- 本文实例为大家分享了python实现飞机大战的具体代码,供大家参考,具体内容如下初学Python,写了一个简单的Python小游戏。师出bi
- #-*- coding:utf-8 -*- from win32com.client import Dispatch if __name__
- 黑体是视觉设计师常用的一款字体,特别是针对广告的 Banner 等。根据 * 的相关介绍,有关黑体的定义可以认为:黑體与白体
- 获取首页元素信息:目标 test_URL:http://www.xxx.com.cn/首先检查元素,a 标签下是我们需要爬取得链接,通过获取
- 第一次写ASP类,实现功能:分段统计程序执行时间,输出统计表等.程序代码:Class ccClsProcessTimeRecord
- pytorch Backward过程用时太长问题描述使用pytorch对网络进行训练的时候遇到一个问题,forward阶段很快(只需要几毫秒
- 1.什么是装饰器?要理解什么是装饰器,您首先需要熟悉Python处理函数的方式。从它的观点来看,函数和对象没有什么不同。它们有属性,可以重新