如何在`return FileResponsefile_path`之后删除文件

问题描述

我正在使用FastAPI接收图像,对其进行处理,然后将图像作为FileResponse返回。

但是返回的文件是临时文件,在端点返回文件后需要删除。

@app.post("/send")
async def send(imagem_base64: str = Form(...)):

    # Convert to a Pillow image
    image = base64_to_image(imagem_base64)

    temp_file = tempfile.mkstemp(suffix = '.jpeg')
    image.save(temp_file,dpi=(600,600),format='JPEG',subsampling=0,quality=85)

    return FileResponse(temp_file)

    # I need to remove my file after return it
    os.remove(temp_file)

返回文件后如何删除文件?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)