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)
相关推荐
charles_vaez几秒前
开源模型应用落地-FastAPI-助力模型交互-WebSocket篇(五)
深度学习·websocket·语言模型·langchain·fastapi
息流使用宝典4 分钟前
TensorFlow 开源的机器学习框架 简介
人工智能·python·tensorflow
zhangkai__11 分钟前
SpringCloud Feign 报错 Request method ‘POST‘ not supported 的解决办法
python·spring·spring cloud
Cpdr27 分钟前
pytorch自适应的调整特征图大小
pytorch·python·深度学习
写代码的中青年30 分钟前
Semantic Kernel:微软大模型开发框架——LangChain 替代
人工智能·python·microsoft·langchain·大模型·llm
ljh_a11 小时前
Django 和 Django REST framework 创建对外 API
python·http·django·flask·tornado
syluxhch1 小时前
Pycharm的终端(Terminal)中切换到当前项目所在的虚拟环境
ide·python·pycharm
yjjpp23011 小时前
Django REST Framework(四)DRF APIVIEW
后端·python·django
铁匠匠匠1 小时前
django学习入门系列之第三点《BootSrap初了解》
前端·经验分享·笔记·python·学习·django·前端框架
2401_857026231 小时前
【Conda与Pip的完美融合】在Conda环境中优雅使用pip指南
python·conda·pip