简体   繁体   中英

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}

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM