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)
相关推荐
酷爱码44 分钟前
如何通过python连接hive,并对里面的表进行增删改查操作
开发语言·hive·python
蹦蹦跳跳真可爱5891 小时前
Python----深度学习(基于深度学习Pytroch簇分类,圆环分类,月牙分类)
人工智能·pytorch·python·深度学习·分类
MinggeQingchun4 小时前
Python - 爬虫-网页解析数据-库lxml(支持XPath)
爬虫·python·xpath·lxml
Python自动化办公社区5 小时前
Python 3.14:探索新版本的魅力与革新
开发语言·python
weixin_贾6 小时前
最新AI-Python机器学习与深度学习技术在植被参数反演中的核心技术应用
python·机器学习·植被参数·遥感反演
张槊哲6 小时前
函数的定义与使用(python)
开发语言·python
船长@Quant6 小时前
文档构建:Sphinx全面使用指南 — 实战篇
python·markdown·sphinx·文档构建
偶尔微微一笑7 小时前
AI网络渗透kali应用(gptshell)
linux·人工智能·python·自然语言处理·编辑器
船长@Quant8 小时前
文档构建:Sphinx全面使用指南 — 基础篇
python·markdown·sphinx·文档构建
喵手9 小时前
从 Java 到 Kotlin:在现有项目中迁移的最佳实践!
java·python·kotlin