当前位置:Gxlcms > Python > Python使用Flask框架同时上传多个文件的方法

Python使用Flask框架同时上传多个文件的方法

时间:2021-07-01 10:21:17 帮助过:54人阅读

本文实例讲述了Python使用Flask框架同时上传多个文件的方法,分享给大家供大家参考。具体如下:

下面的演示代码带有详细的html页面和python代码

  1. import os
  2. # We'll render HTML templates and access data sent by POST
  3. # using the request object from flask. Redirect and url_for
  4. # will be used to redirect the user once the upload is done
  5. # and send_from_directory will help us to send/show on the
  6. # browser the file that the user just uploaded
  7. from flask import Flask, render_template, request, redirect, url_for, send_from_directory
  8. from werkzeug import secure_filename
  9. # Initialize the Flask application
  10. app = Flask(__name__)
  11. # This is the path to the upload directory
  12. app.config['UPLOAD_FOLDER'] = 'uploads/'
  13. # These are the extension that we are accepting to be uploaded
  14. app.config['ALLOWED_EXTENSIONS'] = set(['txt', 'pdf', 'png', 'jpg', 'jpeg', 'gif'])
  15. # For a given file, return whether it's an allowed type or not
  16. def allowed_file(filename):
  17. return '.' in filename and \
  18. filename.rsplit('.', 1)[1] in app.config['ALLOWED_EXTENSIONS']
  19. # This route will show a form to perform an AJAX request
  20. # jQuery is loaded to execute the request and update the
  21. # value of the operation
  22. @app.route('/')
  23. def index():
  24. return render_template('index.html')
  25. # Route that will process the file upload
  26. @app.route('/upload', methods=['POST'])
  27. def upload():
  28. # Get the name of the uploaded files
  29. uploaded_files = request.files.getlist("file[]")
  30. filenames = []
  31. for file in uploaded_files:
  32. # Check if the file is one of the allowed types/extensions
  33. if file and allowed_file(file.filename):
  34. # Make the filename safe, remove unsupported chars
  35. filename = secure_filename(file.filename)
  36. # Move the file form the temporal folder to the upload
  37. # folder we setup
  38. file.save(os.path.join(app.config['UPLOAD_FOLDER'],filename))
  39. # Save the filename into a list, we'll use it later
  40. filenames.append(filename)
  41. # Redirect the user to the uploaded_file route, which
  42. # will basicaly show on the browser the uploaded file
  43. # Load an html page with a link to each uploaded file
  44. return render_template('upload.html', filenames=filenames)
  45. # This route is expecting a parameter containing the name
  46. # of a file. Then it will locate that file on the upload
  47. # directory and show it on the browser, so if the user uploads
  48. # an image, that image is going to be show after the upload
  49. @app.route('/uploads/<filename>')
  50. def uploaded_file(filename):
  51. return send_from_directory(app.config['UPLOAD_FOLDER'],
  52. filename)
  53. if __name__ == '__main__':
  54. app.run(
  55. host="0.0.0.0",
  56. port=int("80"),
  57. debug=True
  58. )</filename>

index.html代码

  1. <div class="container">
  2. <div class="header">
  3. <h3 class="text-muted">How To Upload a File.</h3>
  4. </div>
  5. <hr>
  6. <div>
  7. </div>
  8. </div>

upload.html页面:

  1. <div class="container">
  2. <div class="header">
  3. <h3 class="text-muted">Uploaded files</h3>
  4. </div>
  5. <hr>
  6. <div>
  7. This is a list of the files you just uploaded, click on them to load/download them
  8. <ul>
  9. {% for file in filenames %}
  10. <li>{{file}}</li>
  11. {% endfor %}
  12. </ul>
  13. </div>
  14. <div class="header">
  15. <h3 class="text-muted">Code to manage a Upload</h3>
  16. </div>
  17. <hr>
  18. <pre class="brush:php;toolbar:false layui-box layui-code-view layui-code-notepad"><ol class="layui-code-ol"><li>@app.route('/upload', methods=['POST'])</li><li>def upload():</li><li> # Get the name of the uploaded file</li><li> #file = request.files['file']</li><li> uploaded_files = request.files.getlist("file[]")</li><li> filenames = []</li><li> for file in uploaded_files:</li><li> # Check if the file is one of the allowed types/extensions</li><li> if file and allowed_file(file.filename):</li><li> # Make the filename safe, remove unsupported chars</li><li> filename = secure_filename(file.filename)</li><li> # Move the file form the temporal folder to the upload</li><li> # folder we setup</li><li> file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))</li><li> filenames.append(filename)</li><li> # Redirect the user to the uploaded_file route, which</li><li> # will basicaly show on the browser the uploaded file</li><li> # Load an html page with a link to each uploaded file</li><li> return render_template('upload.html', filenames=filenames)</li><li></li></ol></pre>
  19. </div>

希望本文所述对大家的Python程序设计有所帮助。

人气教程排行