19

I have a function accessible through my REST API, configured with ASP.NET Web API 2.1, that should return an image to the caller. For testing purposes, I just have it returning a sample image I have stored on my local machine right now. Here is the method:

public IHttpActionResult GetImage()
        {
            FileStream fileStream = new FileStream("C:/img/hello.jpg", FileMode.Open);
            HttpContent content = new StreamContent(fileStream);
            content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("image/jpeg");
            content.Headers.ContentLength = fileStream.Length;
            return Ok(content);
         }

When this method gets called, I am not getting an image back at all. Here is the response I am receiving:

{"Headers":[{"Key":"Content-Type","Value":["image/jpeg"]},{"Key":"Content-Length","Value":["30399"]}]}

Why am I not getting the image data back as part of the request? How can that be resolved?

Jim
  • 4,509
  • 16
  • 50
  • 80

3 Answers3

42

One possibility is to write a custom IHttpActionResult to handle your images:

public class FileResult : IHttpActionResult
{
    private readonly string filePath;
    private readonly string contentType;

    public FileResult(string filePath, string contentType = null)
    {
        this.filePath = filePath;
        this.contentType = contentType;
    }

    public Task<HttpResponseMessage> ExecuteAsync(CancellationToken cancellationToken)
    {
        return Task.Run(() =>
        {
            var response = new HttpResponseMessage(HttpStatusCode.OK)
            {
                Content = new StreamContent(File.OpenRead(filePath))
            };

            var contentType = this.contentType ?? MimeMapping.GetMimeMapping(Path.GetExtension(filePath));
            response.Content.Headers.ContentType = new MediaTypeHeaderValue(contentType);

            return response;
        }, cancellationToken);
    }
}

that you could use in your Web API controller action:

public IHttpActionResult GetImage()
{
    return new FileResult(@"C:\\img\\hello.jpg", "image/jpeg");
}
Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
  • 11
    why can't they just provide this out of the box?! – Greg Sep 23 '15 at 00:40
  • 3
    Is `Task.Run` necessary? Won't `Task.FromResult` be enough? – Athari Mar 03 '16 at 18:01
  • @Squidward While Task.FromResult would work, I believe that Task.Run could be slightly more beneficial in this case since it would allow the file to be read in a separate Task. Task.Run also accepts the cancellationToken so there might be some benefit there as well. – Scott May 18 '17 at 19:34
  • 1
    @Scott If the code is fast (calling a couple of constructors is) you lose more CPU time on infrastructure than you get by freeing thread time. – Athari May 24 '17 at 12:16
4

Adding to what @Darin mentions, the Ok<T>(T content) helper which you are using actually returns a OkNegotiatedContentResult<T>, which as the name indicates runs content negotiation. Since you do not want content negotiation in this case, you need to create a custom action result.

Following is one sample of how you can do that: http://aspnet.codeplex.com/SourceControl/latest#Samples/WebApi/ActionResults/ActionResults/Results/OkFileDownloadResult.cs

Kiran
  • 56,921
  • 15
  • 176
  • 161
-1

You can download your file by following code:

    HttpResponse response = HttpContext.Current.Response; 
    response.Clear();
    response.Buffer = false;
    response.BufferOutput = false;
    response.Charset = "UTF-8";
    response.ContentEncoding = System.Text.Encoding.UTF8;           
    response.AppendHeader("Content-disposition", "attachment; filename=" + fileName);
    response.Write(excelXml);
    response.Flush();
    response.End();
    HttpContext.Current.Response.End();
Chandrika Prajapati
  • 867
  • 1
  • 6
  • 11