Return jpeg image from Asp.Net Core WebAPI

被刻印的时光 ゝ 提交于 2019-11-29 00:59:51
hannes neukermans

Clean solution use FilestreamResult !!

[HttpGet]
public async Task<IActionResult> Get()
{
    var image = System.IO.File.OpenRead("C:\\test\\random_image.jpeg");
    return File(image, "image/jpeg");
}

Explanation:

In ASP.NET Core you have to use the built-in File() method inside the Controller. This will allow you to manually set the content type.

Don't create and return HttpResponseMessage, like you were used to using in ASP.NET Web API 2. It doesn't do anything, not even throwing errors!!

Antoine V

PhysicalFile helps to return file from Asp.Net Core WebAPI with a syntax simple

    [HttpGet]
    public IActionResult Get(int imageId)
    {            
       return new PhysicalFile(@"C:\test.jpg", "image/jpeg");
    }
[HttpGet("Image/{id}")]
    public IActionResult Image(int id)
    {
        if(id == null){ return NotFound(); }
        else{

            byte[] imagen = "@C:\\test\random_image.jpeg";
            return File(imagen, "image/jpeg");
        }
    }
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!