Return jpeg image from Asp.Net Core WebAPI
Clean solution use FilestreamResult
!!
[HttpGet]
public 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!!
PhysicalFile helps to return file from Asp.Net Core WebAPI with a syntax simple
[HttpGet]
public IActionResult Get(int imageId)
{
return PhysicalFile(@"C:\test.jpg", "image/jpeg");
}