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)
相关推荐
哥本哈士奇(aspnetx)3 小时前
Streamlit + LangChain 1.0 简单实现智能问答前后端
python·大模型
我一定会有钱4 小时前
斐波纳契数列、end关键字
python
小鸡吃米…5 小时前
Python 列表
开发语言·python
星依网络6 小时前
yolov5实现游戏图像识别与后续辅助功能
python·开源·游戏程序·骨骼绑定
大佐不会说日语~6 小时前
Spring AI Alibaba 的 ChatClient 工具注册与 Function Calling 实践
人工智能·spring boot·python·spring·封装·spring ai
2501_921649496 小时前
如何获取美股实时行情:Python 量化交易指南
开发语言·后端·python·websocket·金融
qq_448011166 小时前
python HTTP请求同时返回为JSON的异常处理
python·http·json
棒棒的皮皮6 小时前
【OpenCV】Python图像处理几何变换之翻转
图像处理·python·opencv·计算机视觉
CodeCraft Studio7 小时前
国产化PPT处理控件Spire.Presentation教程:使用Python将图片批量转换为PPT
python·opencv·powerpoint·ppt文档开发·ppt组件库·ppt api
五阿哥永琪7 小时前
Spring Boot 中自定义线程池的正确使用姿势:定义、注入与最佳实践
spring boot·后端·python