zoukankan      html  css  js  c++  java
  • flask通过form表单一次上传多个文件

    基本上,用了flask官网的示例代码(中文版英文版),稍微做了修改。

    1 import os
    2 from flask import Flask, flash, request, redirect, url_for
    3 from werkzeug.utils import secure_filename
    4 
    5 UPLOAD_FOLDER = '/path/to/the/uploads'
    6 ALLOWED_EXTENSIONS = set(['txt', 'pdf', 'png', 'jpg', 'jpeg', 'gif'])
    7 
    8 app = Flask(__name__)
    9 app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
     1 def allowed_file(filename):
     2     return '.' in filename and 
     3            filename.rsplit('.', 1)[1] in ALLOWED_EXTENSIONS
     4 
     5 @app.route('/', methods=['GET', 'POST'])
     6 def upload_file():
     7     if request.method == 'POST':
     8         for file in request.files.getlist('file'): # 这里改动以存储多份文件
     9             if file and allowed_file(file.filename):
    10                 filename = secure_filename(file.filename)
    11                 file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
    12 
    13     return '''
    14     <!doctype html>
    15     <title>Upload new File</title>
    16     <h1>Upload new File</h1>
    17     <form action="" method=post enctype=multipart/form-data>
    18       <p><input type=file name=file>
    19       <p><input type=file name=file>
    20          <input type=submit value=Upload>
    21     </form>
    22     '''

    这样就可以一次性提交多份文件。具体实现可参看相关源码

  • 相关阅读:
    npm start报错
    npm install 错误
    vue父子组件间传值
    vue-devtools安装过程的坑
    用js进行排序
    筛选表格数据
    基于ElementUI封装可复用的表格组件
    小程序头部滑动切换
    DisneyDiffuse解析
    基于URP的ScreenSpaceDecal的实现(其实和URP没啥关系)
  • 原文地址:https://www.cnblogs.com/lyg-blog/p/9807060.html
Copyright © 2011-2022 走看看