How Python uploads multiple files simultaneously using the Flask framework

Source: Internet
Author: User
Tags unsupported
This example describes how Python uses the flask framework to upload multiple files at the same time and share them for your reference. Specific as follows:

The following demo code comes with a detailed HTML page 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# would be used to Redirect the user once the upload are done# and send_from_directory would help us to s End/show on the# Browser The file, 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__) # The "The path to the upload directoryapp.config[' upload_folder" = ' uploads/' # These is the extension, we are ACCE  Pting to being 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 would show a form to perform an AJAX request# jQuery is loaded t o Execute the request and update the# ValUE of the Operation@app.route ('/') def index (): Return render_template (' index.html ') # route that would process the file UPL Oad@app.route ('/upload ', methods=[' POST ') def upload (): # Get The name of the uploaded files Uploaded_files = request.fi Les.getlist ("file[]") filenames = [] for file with Uploaded_files: # Check If the file is one of the allowed Types/exte  nsions 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 fi      Le.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 # 'll basicaly show on the bro Wser the uploaded file # Load an HTML page with a link to each uploaded file return render_template (' upload.html ', filen Ames=filenames) # this ROute is expecting a parameter containing the name# of a file. Then it would locate this file on the upload# directory and show it in the browser, so if the user uploads# a image, that Image is going to being 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 ("+"), debug=true)
  
 

Index.html Code

   
 
             

How to Upload a File.

Upload.html page:

   
 
  
            

Uploaded files

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[' F Ile ']  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 Filen Ame safe, remove unsupported chars      filename = secure_filename (file.filename)      # Move The file form the temporal fol Der 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      # 'll basicaly show on the bro Wser the uploaded file  # Load an HTML page with a link to each uploaded file  return render_template (' upload.html ', Filenames=filenames)

Hopefully 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.