国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 編程 > Python > 正文

Python使用Flask框架同時(shí)上傳多個(gè)文件的方法

2019-11-25 17:55:13
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

本文實(shí)例講述了Python使用Flask框架同時(shí)上傳多個(gè)文件的方法,分享給大家供大家參考。具體如下:

下面的演示代碼帶有詳細(xì)的html頁(yè)面和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/<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代碼

<!DOCTYPE html><html lang="en"> <head>  <link href="bootstrap/3.0.0/css/bootstrap.min.css"  rel="stylesheet"> </head> <body>  <div class="container">   <div class="header">    <h3 class="text-muted">How To Upload a File.</h3>   </div>   <hr/>   <div>   <form action="upload" method="post" enctype="multipart/form-data">   <input type="file" multiple="" name="file[]" class="span3" /><br/>    <input type="submit" value="Upload" class="span2">   </form>   </div>  </div> </body></html>

upload.html頁(yè)面:

<!DOCTYPE html><html lang="en"> <head>  <link href="bootstrap/3.0.0/css/bootstrap.min.css"     rel="stylesheet"> </head> <body>  <div class="container">   <div class="header">    <h3 class="text-muted">Uploaded files</h3>   </div>   <hr/>   <div>   This is a list of the files you just uploaded, click on them to load/download them   <ul>    {% for file in filenames %}     <li><a href="{{url_for('uploaded_file', filename=file)}}">{{file}}</a></li>    {% endfor %}   </ul>   </div>   <div class="header">    <h3 class="text-muted">Code to manage a Upload</h3>   </div>   <hr/>  <pre>@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)</pre>   </div>  </div> </body></html>

希望本文所述對(duì)大家的Python程序設(shè)計(jì)有所幫助。

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 五河县| 包头市| 米林县| 城步| 郯城县| 沭阳县| 牡丹江市| 黄陵县| 博白县| 天镇县| 武城县| 浪卡子县| 安乡县| 永宁县| 昌江| 大港区| 沙湾县| 华蓥市| 句容市| 贵港市| 兴山县| 黄山市| 松原市| 霸州市| 潼关县| 红安县| 合水县| 淄博市| 靖州| 邳州市| 太谷县| 惠东县| 二连浩特市| 噶尔县| 东乡族自治县| 夏津县| 昌黎县| 左云县| 辽阳县| 介休市| 柏乡县|