Return jpeg image from Asp.Net Core WebAPI

asp net core return image from byte array
handling images in web api
return image from web api c#
web api return multiple images
save image in folder using web api
web api return image from database
ihttpactionresult return image
asp.net core upload image to database

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;
}

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

How To Return An Image Result From Web API Using .NET Core, NET Core and we will learn how to return an image while you hit an API. NET Core as the framework to develop this Web API. AspNetCore.Mvc \\repos\\ KapilDGImage\\KapilDGImage\\Images\\Kapil_Dev_Google.jpg");� Today, we will learn how to return an image from a web API. We will use .NET Core as the framework to develop this Web API. I will be using Visual Studio 2017 for this development activity. By the end of this application, you will be able to render the image from a text through web API and an image file from your server or other hosted location.

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");
    }

Returning Images from ASP.NET Web API, At times you need to store image data directly into a SQL Server database rather than the image URL. While dealing with this scenario you� 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.

In my case, I was using a relative path to the image, so the following was my working solution

[HttpGet]
public async Task<IActionResult> Get()
{
    var url = "/content/image.png";
    var path = GetPhysicalPathFromURelativeUrl(url);
    return PhysicalFile(image, "image/png");
}
public string GetPhysicalPathFromRelativeUrl(string url)
{            
    var path = Path.Combine(_host.Value.WebRootPath, url.TrimStart('/').Replace("/", "\\"));
    return path;
}

Return an image from asp.net web api core as IActionResult, Return an image from asp.net web api core as IActionResult - asp.net-core. For example, you can do: return File("~/Images/photo.jpg", "image/jpeg"); This� I created a mvc4 webapi project using vs2012rc. In my project i wants to render an image. Is it possible to return an image with datatype image from a get method?

[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");
        }
    }

How to save image on the serwer using ASP.NET Core 2.0 — Steemit, You will learn how to correctly save image on the server using ASP.NET Core. NET Core 2.0 Web API project. JPG. project_2.JPG. In the new project I will create new folder with one class. RawFormat)) { return true; } if (ImageFormat. Use the base controllers File method. public ActionResult Image(string id) { var dir = Server.MapPath("/Images"); var path = Path.Combine(dir, id + ".jpg"); //validate the path for security or use other means to generate the path.

Return or Download File in ASP.NET Core WebAPI, NET Core WebAPI or Angular Applications. Using the below technique controller can return any other types of images like .bmp or .jpg etc. Return or Download File in ASP.NET Core WebAPI. In this article, we will see how to download or return useful file types like .pdf, .txt, .xlsx, etc in the browser or any other type of application. We will use ASP.NET Core 3.0 WebAPI as sample.

Return an image from asp.net web api core as IActionResult, 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� This article will help you to return an Image from ASP.NET MVC Controller. In this article, I will use the base controllers File Method. In this article, I will use the base controllers File Method. Let's create one empty MVC project.

Returning an image in Web API method with fallback result , with Microsoft Web API and ASP.NET. First we need to create a controller which inherits ApiController and method which will return an image� An action isn't required to return any specific type. ASP.NET Core supports any object return value. Results from actions that return objects that are not IActionResult types are serialized using the appropriate IOutputFormatter implementation. For more information, see Controller action return types in ASP.NET Core web API.

Comments
  • Visual Studio says that this method lacks await usage and therefor will run synchronously. Is it possible to cache the file so it will not have to be read each time? Why not use the overloaded File method that accepts a file path instead of reading the file? And if reading the file why not use the using statement?
  • writing the line as: return await Task.Run(() => File(image, "image/jpeg")); removed the error for me and also worked.
  • Rewriting the method from 'async Task<IActionResult>' to 'IActionResult' does the trick too in a cleaner way (Visual Studio correctly states that there is asynchronous behavior in this method) ;) ...
  • don't forget to Dispose that OpenRead object, but PhysicalFile is a better solution.
  • Here, you don't need async and Task<> in your method signature.
  • i wonder does this async?, i dont want to be engage with dead-lock or inaccesible-file ?