Vaqif
Vaqif

Reputation: 635

upload image to custom folder (fastapi)

When I try to upload an image, images uploads in the main dir. how can I change the upload destination into the media folder?

@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}

Upvotes: 0

Views: 1153

Answers (1)

salius
salius

Reputation: 1088

I'm not familiar with shutil module, but obviously, you should use

with open(f'my_dir/{single_file.filename}', "wb") as buffer:

Upvotes: 1

Related Questions