Python uses the Flask framework to upload multiple files at the same time,

Source: Internet
Author: User
Tags unsupported

Python uses the Flask framework to upload multiple files at the same time,

This article describes how to upload multiple files simultaneously using the Flask framework in Python. The details are as follows:

The Demo code below contains detailed html pages and python code

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 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(['txt', 'pdf', 'png', 'jpg', 'jpeg', 'gif'])# For a given file, return whether it's 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 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 code

<!DOCTYPE html>

Upload.html page:

<!DOCTYPE html>

I hope this article will help you with Python programming.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.