最新文章专题视频专题问答1问答10问答100问答1000问答2000关键字专题1关键字专题50关键字专题500关键字专题1500TAG最新视频文章推荐1 推荐3 推荐5 推荐7 推荐9 推荐11 推荐13 推荐15 推荐17 推荐19 推荐21 推荐23 推荐25 推荐27 推荐29 推荐31 推荐33 推荐35 推荐37视频文章20视频文章30视频文章40视频文章50视频文章60 视频文章70视频文章80视频文章90视频文章100视频文章120视频文章140 视频2关键字专题关键字专题tag2tag3文章专题文章专题2文章索引1文章索引2文章索引3文章索引4文章索引5123456789101112131415文章专题3
问答文章1 问答文章501 问答文章1001 问答文章1501 问答文章2001 问答文章2501 问答文章3001 问答文章3501 问答文章4001 问答文章4501 问答文章5001 问答文章5501 问答文章6001 问答文章6501 问答文章7001 问答文章7501 问答文章8001 问答文章8501 问答文章9001 问答文章9501
当前位置: 首页 - 科技 - 知识百科 - 正文

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

来源:懂视网 责编:小采 时间:2020-11-27 14:32:10
文档

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

Python使用Flask框架同时上传多个文件的方法:本文实例讲述了Python使用Flask框架同时上传多个文件的方法,分享给大家供大家参考。具体如下: 下面的演示代码带有详细的html页面和python代码 import os # We'll render HTML templates and access data sent by POST #
推荐度:
导读Python使用Flask框架同时上传多个文件的方法:本文实例讲述了Python使用Flask框架同时上传多个文件的方法,分享给大家供大家参考。具体如下: 下面的演示代码带有详细的html页面和python代码 import os # We'll render HTML templates and access data sent by POST #

本文实例讲述了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/')
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代码



 
 
 
 
 
 
 

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 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)
    

    希望本文所述对大家的Python程序设计有所帮助。

    声明:本网页内容旨在传播知识,若有侵权等问题请及时与本网联系,我们将在第一时间删除处理。TEL:177 7030 7066 E-MAIL:11247931@qq.com

    文档

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

    Python使用Flask框架同时上传多个文件的方法:本文实例讲述了Python使用Flask框架同时上传多个文件的方法,分享给大家供大家参考。具体如下: 下面的演示代码带有详细的html页面和python代码 import os # We'll render HTML templates and access data sent by POST #
    推荐度:
    标签: 上传 方法 File
    • 热门焦点

    最新推荐

    猜你喜欢

    热门推荐

    专题
    Top