Return jpeg image from Asp.Net Core WebAPI

前端 未结 4 836
青春惊慌失措
青春惊慌失措 2020-12-08 09:30

Using asp.net core web api, I want to have my controller action method to return an jpeg image stream.
In my current implementation, b

4条回答
  •  伪装坚强ぢ
    2020-12-08 09:56

    Clean solution use FilestreamResult !!

    [HttpGet]
    public async Task 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!!

提交回复
热议问题