欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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)文章

  • 簡(jiǎn)介二分查找算法與相關(guān)的Python實(shí)現(xiàn)示例

    簡(jiǎn)介二分查找算法與相關(guān)的Python實(shí)現(xiàn)示例

    這篇文章主要介紹了二分查找算法與相關(guān)的Python實(shí)現(xiàn)示例,Binary Search同時(shí)也是算法學(xué)習(xí)當(dāng)中最基礎(chǔ)的知識(shí),需要的朋友可以參考下
    2015-08-08
  • Python群發(fā)郵件實(shí)例代碼

    Python群發(fā)郵件實(shí)例代碼

    今天試了試Python發(fā)郵件,突然想到能不能群發(fā)郵件呢?群發(fā)郵件是smtplib的一個(gè)bug,不過最終還是解決了
    2014-01-01
  • Flask與數(shù)據(jù)庫(kù)的交互插件Flask-Sqlalchemy的使用

    Flask與數(shù)據(jù)庫(kù)的交互插件Flask-Sqlalchemy的使用

    在構(gòu)建Web應(yīng)用時(shí),與數(shù)據(jù)庫(kù)的交互是必不可少的部分,本文主要介紹了Flask與數(shù)據(jù)庫(kù)的交互插件Flask-Sqlalchemy的使用,具有一定的參考價(jià)值,感興趣的可以了解一下
    2024-03-03
  • python?繪制3D圖案例分享

    python?繪制3D圖案例分享

    這篇文章主要介紹了python?繪制3D圖案例分享,文章圍繞主題展開詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,需要的小伙伴可以參考一下,希望對(duì)你的學(xué)習(xí)有所幫助
    2022-07-07
  • 如何使用python檢測(cè)某網(wǎng)盤鏈接是否有效

    如何使用python檢測(cè)某網(wǎng)盤鏈接是否有效

    這篇文章主要為大家介紹了使用python檢測(cè)某網(wǎng)盤鏈接是否有效的方法示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2024-01-01
  • 基于python的socket實(shí)現(xiàn)單機(jī)五子棋到雙人對(duì)戰(zhàn)

    基于python的socket實(shí)現(xiàn)單機(jī)五子棋到雙人對(duì)戰(zhàn)

    這篇文章主要為大家詳細(xì)介紹了基于python的socket實(shí)現(xiàn)單機(jī)五子棋到雙人對(duì)戰(zhàn),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2019-06-06
  • Python中的類屬性與實(shí)例屬性的區(qū)別和用法

    Python中的類屬性與實(shí)例屬性的區(qū)別和用法

    在Python中,類屬性和實(shí)例屬性是面向?qū)ο缶幊痰暮诵母拍钪?它們?cè)试S存儲(chǔ)和管理對(duì)象的數(shù)據(jù),并影響對(duì)象的行為,本篇文章中,會(huì)學(xué)習(xí)到類屬性和實(shí)例屬性的概念、區(qū)別以及如何在Python中使用它們,同時(shí)提供大量的示例代碼來更好地理解它們的作用和用法,需要的朋友可以參考下
    2023-11-11
  • Pycharm小白級(jí)簡(jiǎn)單使用教程

    Pycharm小白級(jí)簡(jiǎn)單使用教程

    pycharm是一種Python IDE,能夠幫助我們?cè)诰帉懘a時(shí)提高效率。 這篇文章主要介紹了Pycharm小白級(jí)簡(jiǎn)單使用教程,需要的朋友可以參考下
    2020-01-01
  • Python selenium爬取微博數(shù)據(jù)代碼實(shí)例

    Python selenium爬取微博數(shù)據(jù)代碼實(shí)例

    這篇文章主要介紹了Python selenium爬取微博數(shù)據(jù)代碼實(shí)例,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-05-05
  • 從pandas一個(gè)單元格的字符串中提取字符串方式

    從pandas一個(gè)單元格的字符串中提取字符串方式

    今天小編就為大家分享一篇從pandas一個(gè)單元格的字符串中提取字符串方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2019-12-12

最新評(píng)論