下面的演示代码带有详细的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 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/')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程序设计有所帮助。
