为了方便调试el-upload,使用FastApi写的上传图片接口demo

在使用Element Plus的Upload组件的时候,必须写一个可以调通的接口,因为后端没有开发完成,所以就使用FastApi写了个小demo,自己设置好origins允许的跨域来源,要不Vue会报跨域错误

image.png

from fastapi import FastAPI, File, UploadFile
from fastapi.middleware.cors import CORSMiddleware
app = FastAPI()
origins = ["http://localhost", "http://localhost:3010"]  # 允许的跨域来源
app.add_middleware(
    CORSMiddleware,
    allow_origins=origins,  # 允许跨域的源
    allow_credentials=True,  # 是否允许携带cookie
    allow_methods=["*"],  # 允许的方法,这里设置为所有方法
    allow_headers=["*"],  # 允许的请求头,这里设置为所有请求头
)
@app.post("/upload-image/")
async def upload_image(file: UploadFile):
    # 检查文件是否为图片
    if not file.content_type.startswith("image/"):
        return {"message": "The uploaded file is not an image."}

    # 保存图片到本地
    # with open("received_image.jpg", "wb") as buffer:
    #     await file.seek(0)
    #     await file.readinto(buffer)

    return {"message": "Image successfully uploaded!"}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值