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)
相关推荐
棉猴3 分钟前
python海龟绘图之计算夹角towards()
开发语言·python·turtle·海龟绘图·towards
星马梦缘9 分钟前
强化学习实战8.1——用PPO打赢星际争霸【环境配置与下位机代码】
人工智能·python·jupyter·强化学习·星际争霸·stablebaseline3·starcraft2
qq_1898070310 分钟前
SQL快速查找分组记录数异常的分类_利用HAVING筛选
jvm·数据库·python
m0_7478545212 分钟前
Python模型保存为ONNX格式_跨平台推理部署与加速技巧
jvm·数据库·python
YuanDaima204812 分钟前
Python 数据结构与语法速查笔记
开发语言·数据结构·人工智能·python·算法
海市公约12 分钟前
FastAPI中间件与依赖系统实战指南
中间件·fastapi·依赖注入·异步编程·web架构·代码复用·拦截器模式
粉嘟小飞妹儿12 分钟前
怎么关闭MongoDB不需要的HTTP管理接口及REST API
jvm·数据库·python
qq_2069013913 分钟前
c++如何将浮点数按指定精度写入文本_setprecision用法【实战】
jvm·数据库·python
2401_8654396316 分钟前
如何管理Oracle服务器的内核共享内存_shmmax与shmall计算
jvm·数据库·python
万粉变现经纪人21 分钟前
如何解决 pip install flash-attention 报错 需要 SM_80+(Ampere)架构 问题
python·架构·django·bug·virtualenv·pip·pygame