Flask 中 formdata 上传多个文件
在 Flask 中,我们可以使用 request.files
来获取上传的文件,当用户通过表单上传多个文件时,这些文件会以列表的形式存储在 request.files
中,下面是一个详细的教程,包括小标题和单元表格。
(图片来源网络,侵删)
准备工作
1、安装 Flask:确保已经安装了 Flask,如果没有安装,可以使用以下命令安装:
pip install Flask
2、创建一个简单的 HTML 表单,用于上传多个文件:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF8"> <meta name="viewport" content="width=devicewidth, initialscale=1.0"> <title>文件上传</title> </head> <body> <form action="/upload" method="POST" enctype="multipart/formdata"> <input type="file" name="file1" multiple> <input type="file" name="file2" multiple> <button type="submit">上传</button> </form> </body> </html>
Flask 后端处理文件上传
1、创建一个简单的 Flask 应用:
from flask import Flask, request, render_template_string import os app = Flask(__name__)
2、定义一个路由,用于处理文件上传请求:
@app.route('/upload', methods=['POST']) def upload_files(): file1 = request.files['file1'] # 获取第一个文件对象列表 file2 = request.files['file2'] # 获取第二个文件对象列表 return 'Files uploaded successfully'
3、遍历文件对象列表,将文件保存到指定目录:
def save_files(file_list): for file in file_list: if file: # 确保文件对象不为空 filename = secure_filename(file.filename) # 对文件名进行安全处理,防止包含非法字符 file.save(os.path.join('uploads', filename)) # 将文件保存到 uploads 目录下
4、调用 save_files
函数,处理上传的文件:
if __name__ == '__main__': app.run(debug=True)
完整代码示例
from flask import Flask, request, render_template_string, redirect, url_for, flash, send_from_directory, make_response, jsonify, session, g, render_template, escape, request, Response, stream_with_context, send_file, current_app as app, abort, jsonify, make_response, flash, redirect, url_for, render_template_string, send_from_directory, render_template_string, get_flashed_messages, after_request, before_request, request_started, url_for, get_current_url, request_method, request_args, get_jsonify, jsonify, request_context, has_request_context, request_started, url_for, get_current_url, request_method, request_args, get_jsonify, jsonify, request_context, has_request_context, request_started, url_for, get_current_url, request_method, request_args, get_jsonify, jsonify, request_context, has_request_context, request_started, url_for, get_current_url, request_method, request_args, get_jsonify, jsonify, request_context, has_request_context, request_started, url_for, get_current_url, request_method, request_args, get_jsonify, jsonify, request_context, has_request_context from werkzeug.utils import secure_filename import os from flask import Flask, request, render_template_string import os app = Flask(__name__) @app.route('/upload', methods=['POST']) def upload_files(): file1 = request.files['file1'] # 获取第一个文件对象列表 file2 = request.files['file2'] # 获取第二个文件对象列表 save_files(file1) # 保存第一个文件列表中的文件到指定目录(如:uploads) save_files(file2) # 保存第二个文件列表中的文件到指定目录(如:uploads) return 'Files uploaded successfully' def save_files(file_list): for file in file_list: if file: # 确保文件对象不为空 filename = secure_filename(file.filename) # 对文件名进行安全处理,防止包含非法字符 file.save(os.path.join('uploads', filename)) # 将文件保存到 uploads 目录下if __name__ == '__main__':app.run(debug=True)else:passif __name__ == '__main__':app.run(debug=True)else:passif __name__ == '__main__':app.run(debug=True)else:passif __name__ == '__main__':app.run(debug=True)else:passif __name__ == '__main__':app.run(debug=True)else:passif __name__ == '__main__':app.run(debug=True)else:passif __name__ == '__main__':app.run(debug=True)else:passif __name__ == '__main__':app.run(debug=True)else:passif __name__ == '__main__':app.run(debug=True)else:passif __name__ == '__main__':app.run(debug=True)else:passif __name__ == '__main__':app.run(debug=True)else:passif __name__ == '__main__':app.run(debug=True)else:passif __name__ == '__main__':app.run(debug=True)else:passif __name__ == '__main__':app.run(debug=True)else:passif __name__ == '__main__':app.run(debug=True)else:passif __name__ == '__main__':app.run(debug=True)else:passif __name__ == '__main__':app.run(debug=True)else:passif __name__ == '__main__':app.run(debug=True)else:passif __name__ == '__main__':app.run(debug=True)else:passif __name__ == '__main__':app.run(debug=True)else:passif __name__ == '__main__':app.run(debug=True)else:passif __name__ == '__main__':app.run(debug=True)else:passif __name__ == '__main__':app.run(debug=True)else:passif __name__ == '__main__':app.run(debug=True)else:passif __name__ == '__main__':app.run(debug=True)else:passif __name__ == '__main__':app.run(debug=True)else:passif __name__ == '__main__':app.run(debug=True)else:passif __name__ == '__main__':app.run(debug=True)else:passif __name__ == '__main__':app.run(debug=True)else:passif __name__ == '__main__':app.run(debug=True)else:passif __name__ == '__main__':app.run(debug=True)else:passif __name__ == '__main__':app.run(debug=True)else:passif __name__ == '__main__':app.run(debug=True)else:passif __name__ == '__main__':app.run(debug=True)else:passif __name__ == '__main__':app.run(debug=True)else:passif __name__ == '__main__':app.run(debug=True)else:passif __name__ == '__main__':app
原创文章,作者:未希,如若转载,请注明出处:https://www.kdun.com/ask/479295.html
本网站发布或转载的文章及图片均来自网络,其原创性以及文中表达的观点和判断不代表本网站。如有问题,请联系客服处理。
发表回复