el-upload+python fastAPI实现上传文件

el-upload通过action指定后端接口,并通过name指定传输的文件包裹在什么变量名中

javascript 复制代码
 <el-upload
                    class="upload-demo"
                    drag
                    action="https://ai.zscampus.com/toy/upload"
                    multiple
                    name="fileList"
                    :limit="10"
                    accept=".xlsx, .xls, .txt, .csv, .docx, .pdf, .pptx, .html"
                >
                    <el-icon class="el-icon--upload"><upload-filled /></el-icon>
                    <div class="cm-upload-text">
                        点击或拖动文件到此处上传
                    </div>
                    <div class="el-upload__tip">
                        支持 .txt, .docx, .csv, .xlsx, .pdf, .html, .pptx 类型文件
                    </div>
                    <div class="el-upload__tip">
                        最多支持 10 个文件。单个文件最大 500 MB。
                    </div>
                </el-upload> 

后端代码用FastAPI来写,注意:从request中取出来的变量名要和el-upload中的name指定的变量名保持一致

python 复制代码
@router.post("/uploadMaterial")
async def uploadMaterial(fileList: List[UploadFile] = File(...)):
    writeBytes("./upload",fileList) 
    return {
        'code':200,
        "msg":'success'
    }
    

# 将file写入dirs目录文件
def writeBytes(dirs,fileList):
    for file in fileList:
        bytesFile=file.file.read()
        filename="{}_{}".format(getRandomID(),file.filename)
        if not os.path.exists(dirs):
            os.makedirs(dirs)
        with open(dirs+'/'+ filename, "wb") as f:
            f.write(bytesFile)
相关推荐
谅望者19 分钟前
数据分析笔记07:Python编程语言介绍
大数据·数据库·笔记·python·数据挖掘·数据分析
Geo_V43 分钟前
LangChain Memory 使用示例
人工智能·python·chatgpt·langchain·openai·大模型应用·llm 开发
小呀小萝卜儿1 小时前
2025-11-15 学习记录--Python-LSTM模型定义(PyTorch)
python·学习·lstm
百锦再2 小时前
第15章 并发编程
android·java·开发语言·python·rust·django·go
laufing2 小时前
pyinstaller 介绍
python·构建打包
谅望者2 小时前
数据分析笔记09:Python条件语循环
笔记·python·数据分析
Auspemak-Derafru2 小时前
从U盘损坏中恢复视频文件并修复修改日期的完整解决方案
python
techzhi3 小时前
Intellij idea 注释模版
java·python·intellij-idea
李昊哲小课3 小时前
wsl ubuntu24.04 cuda13 cudnn9 pytorch 显卡加速
人工智能·pytorch·python·cuda·cudnn
温暖名字3 小时前
调用qwen3-omni的api对本地文件生成视频文本描述(批量生成)
python·音视频·qwen·qa问答