Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Return an image from asp.net web api core as IActionResult

What is the best way to return an image file as IActionResult while using asp.net web api core? I tried returning a base64 string and it works fine. But not considered as efficient. Is there a way using which we can return an image file object itself as IActionResult.

like image 615
Satyajit Avatar asked May 18 '17 04:05

Satyajit


People also ask

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 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 get image results back from Web API?

byte[] binaryImage = File. ReadAllBytes(imageUrl); return File(binaryImage , "image/jpeg");

How do I return a file in .NET Core API?

ASP.NET 5 WEB API & Angular 12 You can return a FileContentResult object (Blob) from the server. It'll not get automatically downloaded. You may create an anchor tag in your front-end app programmatically and set the href property to an object URL created from the Blob by the method below.


4 Answers

[Route("getProductImage/v1")]
    [HttpGet]
    public async Task<IActionResult> getProductImage(GetProductImageQueryParam parammodel)
    {
        using (HttpClient client = new HttpClient())
        {
            MNimg_URL = MNimg_URL + parammodel.modelname;
            HttpResponseMessage response = await client.GetAsync(MNimg_URL);
            byte[] content = await response.Content.ReadAsByteArrayAsync();
            //return "data:image/png;base64," + Convert.ToBase64String(content);
            return File(content, "image/png", parammodel.modelname);
        }
    }

In .net core web api you can use the above code

here GetProductImageQueryParam is a class with input parameters

like image 64
Shigil P V Avatar answered Oct 17 '22 10:10

Shigil P V


You can use the various overloads of the File() function in controllers that inherit from Controller or ControllerBase.

For example, you can do:

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.

like image 41
juunas Avatar answered Oct 17 '22 09:10

juunas


A File result is called FileContentResult in NET Core 3.x.

like image 6
Bernoulli IT Avatar answered Oct 17 '22 10:10

Bernoulli IT


You can return image using return file with stream or bytes format or using its image path.

There are few overloaded methods for return File(//parameters); which you can use it in mvc controller's action method.

API Controller

[Route("api/[controller]")]
public class FileController : Controller {

    //GET api/file/id
    [HttpGet("{id}"]
    public async Task<IActionResult> GetFile(string id) {
        var stream = await {{//__get_stream_here__//}};
        var response = File(stream, "application/octet-stream"); // FileStreamResult
        return response;
    }    
}

or

var imageFileStream = System.IO.File.OpenRead("// image path");
return File(imageFileStream, "image/jpeg");

Hope this will help you.

like image 2
Amol Bhor Avatar answered Oct 17 '22 09:10

Amol Bhor