python-flask-上传多个文件并存储

本地环境:win10 / centos6 , python3


flask入门看这里:

python-flask结合bootstrap实现网页小工具实例-半小时速通版_bootstrap flask-CSDN博客

https://blog.csdn.net/pxy7896/article/details/137854455

动态添加和删除表格中的行,看这里:

javascript-动态增加和删除表格的行-CSDN博客

https://blog.csdn.net/pxy7896/article/details/141030235


问题描述

在前台上传1~N个文件,后台接收并存储。

实现效果

点击"添加峰图"按钮,可以不断加行:

实际的html内容类似:

后台则需要接收这些附件(前一个input也要)。

解决方案

在前台构建一个form,在里面放一个table,然后在列中增加文件上传按钮,如下所示:

html 复制代码
<form action="run_command_ab1" id="main_form" >
	<div id="suborderlist" class="row col-md-12">
		<table>
			<!-- 略去一些内容 -->
			<!-- 这是编号 -->
			<td name="rowIdx" style="width:10%;"></td>
			<td style="width:40%;">
				<input name="attach" type="file" id="customFile">
            </td>
		</table>
</form>

提交按钮的id是submitButton,加上点击操作:

javascript 复制代码
$("#submitButton").click(function () {
	// 构造数据
	var formData = new FormData();
	var attachList = document.getElementsByName("rowIdx");
    for (var x=0; x<attachList.length; x++) {
    	// 接收前面input的细节,略
		// 拼接一下文件的id
		var idx = "attach." + attachList[x].innerText.toString();
    	var attachFile = document.getElementById(idx).files[0];
    	if (attachFile == null) {
			// 处理空文件
    		var blob = new Blob([], {type: "text/plain;charset=utf-8"});
    		formData.append("subAttach", blob);
    	} else {
    		formData.append("subAttach", attachFile);
    	}
    }
    // 剩余数据
    // 这里main_form是当前整个form的id
	formData.append("ab1Order", $("#main_form").serialize());
    $.ajax({
        type: 'POST',
        url: "/run_command_ab1",
        data: formData,
		processData: false, // jQuery不要处理发送的数据
        contentType: false, // jQuery不要设置Content-Type请求头
        success: function (resp) {
			// 做一些处理
		}
    });
});

在后台这样写:

python 复制代码
@app.route('/run_command_ab1', methods=['POST'])
def run_command_ab1():
    if request.method == 'POST':
        #print(request.form) 
        # ImmutableMultiDict([('ab1Order', 'sampleId=test&target=313&target=21&target=')])
        # 所以这样提取信息
        info = request.form['ab1Order']
        
        #print(request.files)
        # ImmutableMultiDict([('subAttach', <FileStorage: 'xxx.ab1' ('application/octet-stream')>)])
        # 这样获取文件列表
        files = request.files.getlist('subAttach')
        # 遍历
        for f in files:
            f.save("/this/is/the/path/" + f.filename)
        return '文件已上传成功!'

END.

相关推荐
两万五千个小时1 小时前
落地实现 Anthropic Multi-Agent Research System
人工智能·python·架构
哈里谢顿4 小时前
Python 高并发服务限流终极方案:从原理到生产落地(2026 实战指南)
python
用户83562907805118 小时前
无需 Office:Python 批量转换 PPT 为图片
后端·python
markfeng820 小时前
Python+Django+H5+MySQL项目搭建
python·django
GinoWi20 小时前
Chapter 2 - Python中的变量和简单的数据类型
python
JordanHaidee21 小时前
Python 中 `if x:` 到底在判断什么?
后端·python
ServBay21 小时前
10分钟彻底终结冗长代码,Python f-string 让你重获编程自由
后端·python
闲云一鹤21 小时前
Python 入门(二)- 使用 FastAPI 快速生成后端 API 接口
python·fastapi
Rockbean1 天前
用40行代码搭建自己的无服务器OCR
服务器·python·deepseek
曲幽1 天前
FastAPI + Ollama 实战:搭一个能查天气的AI助手
python·ai·lora·torch·fastapi·web·model·ollama·weatherapi