Return multiple images from a .NET web service

I can return an image as an array of bytes from my .NET web service.

My question is if I want to return more than one image in a single request. For example, my web method currently looks something like this:

public byte[] GetImage(string filename) 

BUT what I am trying to solve is how I could achieve something more:

 public ..?... GetAllMemberStuffInOneHit(string memberID) 

This will return, for example, several images, as well as other information, such as the participant’s name and department.

Can this be done? Please help, thanks.

+4
source share
2 answers

That's right. Instead of returning only a byte array, create a DTO class with a byte array, as well as potentially other useful information (file name, content type, etc.). Then just return the IList of them. Something like that:

 public class MyImage { public byte[] ImageData { get; set; } public string Name { get; set; } public MyImage() { // maybe initialize defaults here, etc. } } public List<MyImage> GetAllMemberStuffInOneHit(string memberID) { // implementation } 

Since your method name means that you want to return even more information, you can create more DTO classes and put them together to create a member object. Something like that:

 public class Member { public List<MyImage> Images { get; set; } public string Name { get; set; } public DateTime LastLogin { get; set; } // etc. } public Member GetAllMemberStuffInOneHit(string memberID) { // implementation } 
+4
source

A simple solution is to simply return the List<byte[]> collection, for example:

 [WebMethod] public List<byte[]> GetAllImages(string memberID) { List<byte[]> collection = new List<byte[]>(); // fetch images one at a time and add to collection return collection; } 

To use this, you will need this line at the top of your Service.cs file:

 using System.Collections.Generic; 
+1
source

All Articles