3

I am working on a web API with MVC and C#, I try to return a list of images which are stored in folder and paths of them in the database, I'm trying that:

 public class Image {
    public int Id { get; set; }
    public string Path { get; set; }
    public int Item_Id { get; set; }
    public bool isMain { get; set; }
}

and in image controller I call this method:

 [HttpGet]
    [ActionName("GetImageByItemId")]
    public HttpResponseMessage GetImages(int id)
    {
        try
        {
            using (var ctx = new ApplicationDbContext())
            {
                var entity = ctx.Images.Where(e => e.Item_Id == id).ToList();

                // ctx.Images.FirstOrDefault(e => e.Item_Id == id);
                if (entity != null)
                {
                    List<HttpResponseMessage> shapes = new List<HttpResponseMessage>();
                    HttpResponseMessage response = new HttpResponseMessage();
                    for (int i = 0; i < entity.Count; i++)
                    { 

                    String filePath = HostingEnvironment.MapPath("~/Images/" + entity[i].Path + ".jpg");
                    FileStream fileStream = new FileStream(filePath, FileMode.Open);
                    response.Content = new StreamContent(fileStream); // this file stream will be closed by lower layers of web api for you once the response is completed.
                    response.Content.Headers.ContentType = new MediaTypeHeaderValue("image/png");
                    shapes.Add(response);
                    }
                     // return response;
                    // return Request.CreateResponse(HttpStatusCode.OK, shapes);
                    return ControllerContext.Request
        .CreateResponse(HttpStatusCode.OK, new {shapes});
                }
                else
                {
                    return Request.CreateErrorResponse(HttpStatusCode.NotFound, "The Images With ID" + id.ToString() + " Not Found");
                }
            }
        }
        catch (Exception ex)
        {
            return Request.CreateErrorResponse(HttpStatusCode.BadRequest, ex);
        }
    }

if I use

return response;

or

return  shapes[1];

it's work to return one image but I need it return a list of image, how to do that?

  • 1
    Possible duplicate of [How to download multiple files with one HTTP request?](http://stackoverflow.com/questions/1041542/how-to-download-multiple-files-with-one-http-request) – SandRock Apr 15 '17 at 18:46
  • @SandRock I not need to download multiple files, I need to return list of images to show them in another application that use this wep API. –  Apr 15 '17 at 18:52

1 Answers1

0

One HTTP response can contain zero or one (1) content. No more.

The logical solution

If you want to serve multiple files, you will need to make as many requests as necessary.

First, make a MVC action that will return a list of the files available for a given entity id.

Then, make a MVC action that will take the entity id and the file id as parameter.

On the client side, start by calling the first method. Then call the second one as many times as there are files to download.

The package solution

You can also package multiple files into one file (zip, tar, 7zip...) using System.IO.Packaging or a known library.

The alien solution

You may also try to implement the MIME specification on both server and client side. Be warned: current web browsers do not implement it. This clearly is non-standard therefore not recommended. This article seems to address the technique.

Community
  • 1
  • 1
SandRock
  • 5,276
  • 3
  • 30
  • 49
  • No, I need to show them in another application. So are there any other method to do that and return a list of images? –  Apr 15 '17 at 18:51