Return an image from asp.net web api core as IActionResult

后端 未结 4 822
太阳男子
太阳男子 2021-01-11 16:58

What is the best way to return an image file as IActionResult while using asp.net web api core? I tried returning a base64 string and it works fine. But not considered as ef

4条回答
  •  無奈伤痛
    2021-01-11 17:34

    You can use the various overloads of the File() function in controllers that inherit from Controller or ControllerBase.

    For example, you can do:

    return File("~/Images/photo.jpg", "image/jpeg");
    

    This uses a virtual path, other options include giving it a byte array or a Stream. You can also give a download file name as a third argument if that is needed.

提交回复
热议问题