200字范文,内容丰富有趣,生活中的好帮手!
200字范文 > Python使用Flask框架同时上传多个文件办法

Python使用Flask框架同时上传多个文件办法

时间:2022-01-04 21:08:52

相关推荐

Python使用Flask框架同时上传多个文件办法

后端开发|Python教程

Python,Flask框架,上传,多个文件,方法

后端开发-Python教程

本文实例讲述了Python使用Flask框架同时上传多个文件的方法,分享给大家供大家参考。具体如下:

php7源码下载,在ubuntu下载搜狗,爬虫颤音评论抓取,php鞋,湘潭seo专业lzw

下面的演示代码带有详细的html页面和python代码

asp任务站整站源码,VSCode自动打开终端,ubuntu 挂载sd,tomcat进程死掉,sqlite自定义约束,狂热运输2保护爬虫类,php的redis手册,黄石seo优化服务,彩虹网站监控源码,单网页网站源码,51job模板lzw

import os# Well 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 uploadedfrom flask import Flask, render_template, request, redirect, url_for, send_from_directoryfrom werkzeug import secure_filename# Initialize the Flask applicationapp = Flask(__name__)# This is the path to the upload directoryapp.config[UPLOAD_FOLDER] = uploads/# These are the extension that we are accepting to be uploadedapp.config[ALLOWED_EXTENSIONS] = set([ xt, pdf, png, jpg, jpeg, gif])# For a given file, return whether its an allowed type or notdef 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 charsfilename = secure_filename(file.filename)# Move the file form the temporal folder to the upload# folder we setupfile.save(os.path.join(app.config[UPLOAD_FOLDER],filename))# Save the filename into a list, well use it laterfilenames.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/)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 )

mysql源码工具,ubuntu用锐捷,蜘蛛 也叫爬虫,$x php,MIP与SEOlzw

index.html代码

How To Upload a File.

upload.html页面:

Uploaded files

This is a list of the files you just uploaded, click on them to load/download them {% for file in filenames %}{{file}} {% endfor %}

Code to manage a Upload

@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 charsfilename = secure_filename(file.filename)# Move the file form the temporal folder to the upload# folder we setupfile.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)

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。