2

I have a simple site (WebAPI) that returns a bunch of albums in the get method. Each album has attributes such as title, artist and so on. The attribute under here is the image (album photo) attribute. Each album has an image, what is the best way to send the image back to client. Should it be sent as binary data as part of the album object for example like

Public Class Album
{
    string title;
    byte[] image;
}

Or should I send the path to the image in the Album object and have the client download the image separately?

Like Public Class Album { string title; string imagePath; }

user3547774
  • 1,621
  • 3
  • 20
  • 46

2 Answers2

0

You can see this post The downloaded file as stream in controller (ASP.NET MVC 3) will automatically disposed? as reference.

You can use FileStream.

protected override void WriteFile(HttpResponseBase response) {
    // grab chunks of data and write to the output stream
    Stream outputStream = response.OutputStream;
    using (FileStream) {
        byte[] buffer = new byte[_bufferSize];
        while (true) {
            int bytesRead = FileStream.Read(buffer, 0, _bufferSize);
            if (bytesRead == 0) {
                // no more data
                break;
            }
            outputStream.Write(buffer, 0, bytesRead);
        }
    }
}
Community
  • 1
  • 1
Michael Sync
  • 4,834
  • 10
  • 40
  • 58
  • Thanks for the pointer. Can you also tell me what is the best approach downloading the image separately in a different request and sending the path in the original object or sending the image bytes in the original objects itself? – user3547774 Sep 25 '14 at 09:44
  • Do you want user to download your images? Like user will click on "download" button and those images will be downloaded to user's machine.. Or, do you just display those images on client site (html and js)? If you just want to display those images on HTML page then it makes sense to return the list of image links.. – Michael Sync Sep 25 '14 at 10:00
  • I just want to display the images on the client side. So what you are saying is to send the likn urls to the images and then download each individual image separately? – user3547774 Sep 25 '14 at 10:24
  • you can send the url from api.. then in client side, you set those url to image tag.. the browser will download the image automatically. – Michael Sync Sep 25 '14 at 11:10
0

Instead of passing

Public class Album
{
    string title;
    byte[] image;
}

back to the client, I would change image to a int imageId:

Public class Album
{
    string Title{get;set};
    int ImageId{get;set};
}

Then create a WebApi controller that handles images with a method written like this:

public async Task<HttpResponseMessage> Get(HttpRequestMessage request, int imageId)
    {

        byte[] img = await _myRepo.GetImgAsync(imageId);

        HttpResponseMessage msg = new HttpResponseMessage(HttpStatusCode.OK)
        {
            Content = new ByteArrayContent(img)
        };
        msg.Content.Headers.ContentType = new MediaTypeHeaderValue("image/png");

        return msg;
    }
Mike_G
  • 16,237
  • 14
  • 70
  • 101