mvc 4 web api返回多个图像 [英] mvc 4 web api return multiple images

查看:366
本文介绍了mvc 4 web api返回多个图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在mvc 4 web api中遇到了一个问题。

我想发送逗号分隔图片ID,如1,2,3,并希望使用mvc 4 web api获取所有3张图片。





i我现在正在使用该代码



I am facing an issue in mvc 4 web api .
I want to sent comma seprated image id like 1,2,3 and want to get all 3 images using mvc 4 web api.


i am using that code now

public class ImageServiceController : ApiController
    {
        public HttpResponseMessage Get(string id)
        {
            try
            {
                var result = new HttpResponseMessage(HttpStatusCode.OK);
                String[] strArray = id.Split(',');
                int[] intArray = new int[strArray.Length];
                
               
                for (int i = 0; i < strArray.Length; i++)
                {
                    intArray[i] = int.Parse(strArray[i]);
                    String filePath = HostingEnvironment.MapPath("~/Images/" + intArray[i] + ".png");
                    FileStream fileStream = new FileStream(filePath, FileMode.OpenOrCreate);
                    Image image = Image.FromStream(fileStream);
                    MemoryStream memoryStream = new MemoryStream();
                    image.Save(memoryStream, ImageFormat.Png);
                    result.Content = new ByteArrayContent(memoryStream.ToArray());
                    result.Content.Headers.ContentType = new MediaTypeHeaderValue("image/png");
                    fileStream.Close();
                    memoryStream.Dispose();
                }
                return result;
            }
            catch (Exception ex)
            {
                return new HttpResponseMessage(HttpStatusCode.NotFound);
            }
        }





提前致谢



Thanks in advance

推荐答案

这篇关于mvc 4 web api返回多个图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆