ChatGPT解决这个技术问题 Extra ChatGPT

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

https://i.stack.imgur.com/bMt50.png


h
hannes neukermans

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


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' 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.
changed code so that async warning is no longer shown in vs @DotBert
n
nfplee

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

i wonder does this async?, i dont want to be engage with dead-lock or inaccesible-file ?
@ASLIM The method PhysicalFile() return an instance of type PhysicalFileResult which in return supports execute-async. So if I understand correctly the ASP.NET infrastructure will pick that async method which in turn will invoke PhysicalFileResultExecutor.ExecuteAsync() -> response.SendFileAsync(). So I guess the async part is taken care of transparently.
M
Mustafamg

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

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

I am not sure why they down vote this one all what I see is a working code