簡體   English   中英

將圖像上傳到自定義文件夾(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:

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM