Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Return jpeg image from Asp.Net Core WebAPI

Using asp.net core web api, I want to have my controller action method to return an jpeg image stream.
In my current implementation, browser displays only a json string. My expectation is to see the image in the browser.

While debugging using chrome developer tools I found that the content type is still

Content-Type:application/json; charset=utf-8

returned in the response header, even though in my code I manually set the content type to "image/jpeg".

Looking for a solution My Web API is as below

[HttpGet] public async Task<HttpResponseMessage> Get() {     var image = System.IO.File.OpenRead("C:\\test\random_image.jpeg");     var stream = new MemoryStream();      image.CopyTo(stream);     stream.Position = 0;                 result.Content = new StreamContent(image);     result.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment");     result.Content.Headers.ContentDisposition.FileName = "random_image.jpeg";     result.Content.Headers.ContentType = new MediaTypeHeaderValue("image/jpeg");     result.Content.Headers.ContentLength = stream.Length;      return result; } 

enter image description here

like image 999
hannes neukermans Avatar asked Nov 24 '16 20:11

hannes neukermans


People also ask

How can I return images from Web API in asp net?

In order to return images stored directly in a SQL Server database from an ASP.NET Web API you need to create a Get() method that returns HttpResponseMessage. The Content property of the HttpResponseMessage class represents the binary image data associated with an image.

How do I return an image in .NET Core?

NET Core. In . NET Core, you don't need to create " HttpResponseMessage " to return images from API, it will not return you data, also it will not throw any error, you have to use File() method directly inside the Controller. Basically, the API will accept a query string which is the file name.

How do I return content in Web API?

Depending on which of these is returned, Web API uses a different mechanism to create the HTTP response. Convert directly to an HTTP response message. Call ExecuteAsync to create an HttpResponseMessage, then convert to an HTTP response message. Write the serialized return value into the response body; return 200 (OK).


2 Answers

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!!

like image 158
hannes neukermans Avatar answered Sep 21 '22 17:09

hannes neukermans


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");     } 
like image 23
Antoine V Avatar answered Sep 19 '22 17:09

Antoine V