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)
相关推荐
人道领域4 分钟前
【黑马点评日记】社交平台用户关注功能全解析Feed流相关操作
java·开发语言·数据库·redis·python
Andy Dennis5 分钟前
mcp python-sdk使用记录
python·agent·mcp
zhoutongsheng17 分钟前
mysql如何处理表空间碎片问题_执行OPTIMIZE TABLE整理
jvm·数据库·python
狼与自由21 分钟前
Harness
python
Python私教26 分钟前
从PySide6到Rich+FastAPI:如意Agent终端版架构重构全记录
重构·架构·fastapi
IT策士37 分钟前
Python mcp研究:入门到精通
开发语言·python·qt
罗技12339 分钟前
告别“兼容模式“:Easysearch 有了自己的官方 Python 客户端
开发语言·python
IT策士41 分钟前
Python 常见的设计模型:入门到精通
开发语言·python
PSLoverS1 小时前
Python如何实现测试场景编排_基于pytest的数据驱动组合策略
jvm·数据库·python