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)
相关推荐
DXM052130 分钟前
第13期|遥感语义分割模型:U-Net核心原理+遥感落地优势
人工智能·python·深度学习·目标检测·随机森林·机器学习·支持向量机
码来的小朋友35 分钟前
[python] 我开发了一个有20个关卡随机地图的迷宫游戏
python·游戏·pygame
放下华子我只抽RuiKe539 分钟前
FastAPI 全栈后端(三):数据库与 ORM
前端·数据库·react.js·oracle·性能优化·前端框架·fastapi
夏天测39 分钟前
微信小程序自动化漏洞挖掘流水线:从缓存提取到密钥验证全流程实战
python·网络安全·微信小程序·漏洞挖掘
叫我:松哥1 小时前
基于Python的共享单车租赁数据分析与预测系统,技术栈flask+boostrap+随机森林+XGBoost
人工智能·python·深度学习·算法·随机森林·数据分析·flask
Li#1 小时前
web端电商项目自动下单发货评价晒图需要用到的能力
python·自动化
雨辰AI1 小时前
从零搭建大模型本地运行环境|Python+CUDA 基础配置避坑大全
大数据·开发语言·人工智能·python·ai·ai编程·ai写作
DogDaoDao2 小时前
【第 05 篇】Python的字典与集合
开发语言·python·集合·字典
涛声依旧-底层原理研究所2 小时前
混合检索 + 重排:让 AI Agent 拥有「既全又准」的认知骨架
人工智能·python
努力写A题的小菜鸡2 小时前
01-PyTorch加载数据初认识(dataset运用)
人工智能·pytorch·python