上传图片到自定义文件夹(fastapi)
upload image to custom folder (fastapi)
当我尝试上传图片时,图片会上传到主目录中。如何将上传目标更改为媒体文件夹?
@router.post('/icon', status_code=status.HTTP_201_CREATED,)
async def create_file(single_file: UploadFile = File(...)):
with open(single_file.filename, "wb") as buffer:
shutil.copyfileobj(single_file.file, buffer)
return {"filename": single_file}
我不熟悉 shutil
模块,但显然,您应该使用
with open(f'my_dir/{single_file.filename}', "wb") as buffer:
当我尝试上传图片时,图片会上传到主目录中。如何将上传目标更改为媒体文件夹?
@router.post('/icon', status_code=status.HTTP_201_CREATED,)
async def create_file(single_file: UploadFile = File(...)):
with open(single_file.filename, "wb") as buffer:
shutil.copyfileobj(single_file.file, buffer)
return {"filename": single_file}
我不熟悉 shutil
模块,但显然,您应该使用
with open(f'my_dir/{single_file.filename}', "wb") as buffer: