Returning more than one image from a .NET web service
I'm able to return an image as byte array from my .NET web service..
My question is this, what if i want to return more than a single image in one request. For example, my web method currently looks something like this:
public byte[] GetImage(string filename)
BUT what I'm trying to work out is how i开发者_JAVA技巧 would achieve something more like this:
public ..?... GetAllMemberStuffInOneHit(string memberID)
This would return, for example, a few images, along with some other information such as a members name and department.
Can this be done? Please help, Thank you.
Absolutely. Instead of returning just a byte array, create a DTO class that has the byte array as well as potentially other useful information (file name, content type, etc.). Then just return an IList of those. Something like this:
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 implies that you want to return even more information, you could create more DTO classes and build them together to create a "member" object. Something like this:
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
}
A simple solution is to just return a List<byte[]>
collection, like this:
[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'll need this line at the top of your Service.cs
file:
using System.Collections.Generic;
精彩评论