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

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

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

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

下面的演示代碼帶有詳細(xì)的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>

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

相關(guān)文章

  • Python操作MongoDB詳解及實(shí)例

    Python操作MongoDB詳解及實(shí)例

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

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

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

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

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

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

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

    python 把列表轉(zhuǎn)化為字符串的方法

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

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

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

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

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

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

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

    Python基礎(chǔ)教程之淺拷貝和深拷貝實(shí)例詳解

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

    OpenCV黑帽運(yùn)算(BLACKHAT)的使用

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

最新評(píng)論

调兵山市| 阿勒泰市| 桦甸市| 郯城县| 津南区| 新乡县| 远安县| 威信县| 五寨县| 宁南县| 玉山县| 开原市| 北川| 绥德县| 西平县| 巍山| 禹城市| 平安县| 从化市| 五家渠市| 南宫市| 阿拉善右旗| 固原市| 遂川县| 怀仁县| 莱西市| 汽车| 封丘县| 濉溪县| 永丰县| 小金县| 雷波县| 从化市| 德州市| 宁化县| 康平县| 元氏县| 长顺县| 霍林郭勒市| 大新县| 博罗县|