最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Python使用Flask框架同時上傳多個文件的方法

 更新時間:2015年03月21日 15:37:18   作者:niuniu  
這篇文章主要介紹了Python使用Flask框架同時上傳多個文件的方法,實例分析了Python中Flask框架操作文件實現(xiàn)上傳的技巧,需要的朋友可以參考下

本文實例講述了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/<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頁面:

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

希望本文所述對大家的Python程序設計有所幫助。

相關文章

  • Python操作MongoDB詳解及實例

    Python操作MongoDB詳解及實例

    這篇文章主要介紹了Python操作MongoDB詳解及實例的相關資料,需要的朋友可以參考下
    2017-05-05
  • 對Python新手編程過程中如何規(guī)避一些常見問題的建議

    對Python新手編程過程中如何規(guī)避一些常見問題的建議

    這篇文章中作者對Python新手編程過程中如何規(guī)避一些常見問題給出了建議,主要著眼于初學者對于一些常用函數(shù)方法在平時的使用習慣中的問題給出建議,需要的朋友可以參考下
    2015-04-04
  • Python結合多線程與協(xié)程實現(xiàn)高效異步請求處理

    Python結合多線程與協(xié)程實現(xiàn)高效異步請求處理

    在現(xiàn)代Web開發(fā)和數(shù)據(jù)處理中,高效處理HTTP請求是關鍵挑戰(zhàn)之一,本文將結合Python異步IO(asyncio)和多線程技術,探討如何優(yōu)化請求處理邏輯,解決常見的線程事件循環(huán)問題,有需要的小伙伴可以根據(jù)需求進行選擇
    2025-04-04
  • Python中的 No Module named ***問題及解決

    Python中的 No Module named ***問題及解決

    這篇文章主要介紹了Python中的 No Module named ***問題及解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-07-07
  • python 把列表轉化為字符串的方法

    python 把列表轉化為字符串的方法

    今天小編就為大家分享一篇python 把列表轉化為字符串的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-10-10
  • Python使用遺傳算法解決最大流問題

    Python使用遺傳算法解決最大流問題

    這篇文章主要為大家詳細介紹了Python使用遺傳算法解決最大流問題,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-01-01
  • Python中存取文件的4種不同操作

    Python中存取文件的4種不同操作

    這篇文章主要給大家介紹了關于Python中存取文件的4種不同操作的相關資料,分別包括Python內(nèi)置方法、numpy模塊方法、os模塊方法以及csv模塊方法,文中通過示例代碼介紹的非常詳細,需要的朋友可以參考下
    2018-07-07
  • python3的url編碼和解碼,自定義gbk、utf-8的例子

    python3的url編碼和解碼,自定義gbk、utf-8的例子

    今天小編就為大家分享一篇python3的url編碼和解碼,自定義gbk、utf-8的例子,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2019-08-08
  • Python基礎教程之淺拷貝和深拷貝實例詳解

    Python基礎教程之淺拷貝和深拷貝實例詳解

    這篇文章主要介紹了Python基礎教程之淺拷貝和深拷貝實例詳解的相關資料,需要的朋友可以參考下
    2017-07-07
  • OpenCV黑帽運算(BLACKHAT)的使用

    OpenCV黑帽運算(BLACKHAT)的使用

    本文主要介紹了OpenCV黑帽運算(BLACKHAT)的使用,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2022-08-08

最新評論

大荔县| 柳江县| 宝山区| 达拉特旗| 嘉荫县| 仪陇县| 剑河县| 万宁市| 梅河口市| 牟定县| 平远县| 六枝特区| 广元市| 乌拉特后旗| 康平县| 鸡泽县| 和田市| 沙雅县| 沿河| 阿图什市| 东乌珠穆沁旗| 宜川县| 红河县| 文山县| 金昌市| 托里县| 清镇市| 武隆县| 共和县| 陆川县| 平湖市| 凤冈县| 宁津县| 温泉县| 惠东县| 明星| 邵武市| 望谟县| 南投县| 永春县| 临武县|