@app.post("/ai_read_meter")
async def refresh_capacity_energy_db(request: Request, file: bytes = File(...)):
BASE_DIR = Path(__file__).resolve().parent
"""单文件上传(不能大于5M)"""
if len(file) > 5 * 1024 * 1024:
raise HTTPException(status_code=400, detail="每个文件都不能大于5M")
name = hashlib.md5(file).hexdigest() + ".png" # 使用md5作为文件名,以免同一个文件多次写入
subpath = "media/uploads"
# 如果不存在文件夹,则创建
if not (folder := BASE_DIR / subpath).exists():
await AsyncPath(folder).mkdir(parents=True)
# 如果不存在文件,则写入
if not (fpath := folder / name).exists():
await AsyncPath(fpath).write_bytes(file)
# image = np.asarray(bytearray(file), dtype="uint8")
# start(i=2, image=cv2.imdecode(image, cv2.IMREAD_COLOR))
return f"{subpath}/{name}"
fastapi文件上传
原创
©著作权归作者所有:来自51CTO博客作者wx5c08995b28fc1的原创作品,请联系作者获取转载授权,否则将追究法律责任

提问和评论都可以,用心的回复会被更多人看到
评论
发布评论
相关文章
-
FastApi-14-文件上传-2
获取文件信息!
html 文件大小 字段 -
FastApi-13-文件上传-1
实用FastApi上传文件!
html 文件大小 文件上传 -
FastAPI(62)- FastAPI 部署在 Docker
FastAPI(62)- FastAPI 部署在 Docker
FastAPI docker 缓存 ico 应用程序 -
FastAPI 01--快速入门FastApi
FastAPI 01--快速入
快速入门FastApi FastApi Python3 高性能web框架 python