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)
相关推荐
FY_20189 小时前
Stable Baselines3中调度函数转换器get_schedule_fn 函数
开发语言·人工智能·python·算法
Coder_Boy_9 小时前
【物联网技术】- 基础理论-0001
java·python·物联网·iot
FY_20189 小时前
SubprocVecEnv 原理、详细使用方法
人工智能·python·机器学习
czliutz9 小时前
使用pdfplumber库处理pdf文件获取文本图片作者等信息
python·pdf
Sunhen_Qiletian9 小时前
《Python开发之语言基础》第七集:库--时间库
前端·数据库·python
smile_Iris10 小时前
Day 30 函数定义与参数
开发语言·python
杨航 AI10 小时前
FORCE_VERIFYING_SIGNATURE=false
python
AI弟10 小时前
推荐系统:带你走进推荐之路(二)
人工智能·python·深度学习·面试·推荐算法
不错就是对10 小时前
mmdetection - Linux环境搭建
图像处理·人工智能·python·深度学习·计算机视觉
背心2块钱包邮10 小时前
第5节——定积分与面积
python·深度学习·机器学习·matplotlib