C# MVC WebAPI返回多个图像

C# MVC WebAPI返回多个图像,c#,asp.net-mvc,C#,Asp.net Mvc,我在一个目录中有3个图像,但我的代码总是返回其中一个。我想返回3张图片image1.jpg、image2.jpg、image3.jpg,并在我的Xamarin应用程序中获取它们 我认为像数组一样返回结果可能会解决问题,但我不明白我需要什么 var result = new HttpResponseMessage(HttpStatusCode.OK); MemoryStream ms = new MemoryStream(); for (int i

我在一个目录中有3个图像,但我的代码总是返回其中一个。我想返回3张图片image1.jpg、image2.jpg、image3.jpg,并在我的Xamarin应用程序中获取它们

我认为像数组一样返回结果可能会解决问题,但我不明白我需要什么

        var result = new HttpResponseMessage(HttpStatusCode.OK);

        MemoryStream ms = new MemoryStream();
        for (int i = 0; i < 3; i++)
        {
            String filePath = HostingEnvironment.MapPath("~/Fotos/Empresas/Comer/" + id + (i + 1) + ".jpg");

            FileStream fileStream = new FileStream(filePath, FileMode.OpenOrCreate);

            Image image = Image.FromStream(fileStream);
            image.Save(ms, ImageFormat.Jpeg);
            fileStream.Close();

            byte[] bytes = File.ReadAllBytes(filePath);
            byte[] length = BitConverter.GetBytes(bytes.Length);

            // Write length followed by file bytes to stream
            ms.Write(length, 0, 3);
            ms.Write(bytes, 0, bytes.Length);
        }

        result.Content = new StreamContent(ms);

        return result;

这是我获取图像的xamarin代码,但我仍然没有得到任何东西=/

在响应中需要一些东西来界定每个图像的开始和结束位置。作为一个基本的解决方案,您可以将图像长度写为Int32,并跟随图像数据。在另一端,您需要读取4字节的长度,后跟x字节数:

[HttpGet]
public HttpResponseMessage Get(string id)
{
    var result = new HttpResponseMessage(HttpStatusCode.OK);
    String[] strArray = id.Split(',');

    var ms = new MemoryStream();
    for (int i = 0; i < strArray.Length; i++)
    {
        String filePath = HostingEnvironment.MapPath("~/Fotos/Empresas/Comer/" + strArray[i] + (i + 1) + ".jpg");

        byte[] bytes = File.ReadAllBytes(filePath);
        byte[] length = BitConverter.GetBytes(bytes.Length);

        // Write length followed by file bytes to stream
        ms.Write(length, 0, 4);
        ms.Write(bytes, 0, bytes.Length);
    }

    result.Content = new StreamContent(ms);

    return result;
}
[HttpGet]
公共HttpResponseMessage获取(字符串id)
{
var结果=新的HttpResponseMessage(HttpStatusCode.OK);
字符串[]strArray=id.Split(',');
var ms=新内存流();
for(int i=0;i
如果您只返回一个memorystream,则不容易区分流中的一个图像和另一个图像,相反,您可以返回一个字节数组列表,然后您可以访问数组中的每个位置并从字节数组转换为图像

这是一个功能齐全的dotnet core webapi控制器:

public class GetImagesController : Controller
{
    private readonly IWebHostEnvironment _host;

    public GetImagesController(IWebHostEnvironment host)
    {
        _host = host;
    }

    [HttpGet("{images}")]
    public async Task<List<byte[]>> Get([FromQuery]string images)
    {
        List<byte[]> imageBytes = new List<byte[]>();
        String[] strArray = images.Split(',');
        for (int i = 0; i < strArray.Length; i++)
        {
            String filePath = Path.Combine(_host.ContentRootPath, "images", strArray[i]+".jpg");
            byte[] bytes =  System.IO.File.ReadAllBytes(filePath);
            imageBytes.Add(bytes);

        }
        return imageBytes;
    }
}
公共类GetImagesController:控制器
{
专用只读IWebHostEnvironment\u主机;
公共GetImagesController(IWebHostEnvironment主机)
{
_主机=主机;
}
[HttpGet(“{images}”)]
公共异步任务获取([FromQuery]字符串图像)
{
List imageBytes=新列表();
字符串[]strArray=images.Split(',');
for(int i=0;i
此控制器可以这样调用:

假设您有一个名为images的文件夹,在ContentRooPath下有文件P1.jpg、P2.jpg和P3.jpg


考虑返回一个包含3字节数组的对象。使用File.ReadAllBytes()将整个图像文件读取到服务器端的字节数组中。前端Xamarin客户端必须将每个字节数组转换为一个映像。需要注意的是,WebAPI调用返回的总字节数有限制。result.Content=new byteArray内容(memoryStream.ToArray());每次通过循环时都将覆盖result.Content的当前值。我希望每次调用它时它都会返回最后处理的图像。我想我在xamarin中得到了错误的方式,我将在编辑中显示我转换字节的方式,在debbug中我看到得到了所有字节,但当我在IIS中打开时,什么都不显示,在这种情况下,这是正常的,因为我只有字节,在我的例子中,我只需要将字节从我的xamarin中的结果转换为图像,对吗?没错-如果你尝试在浏览器中打开图像,它不会显示。现在我坚持将字节放在xamarin中的列表中,返回显示一个数组,但里面不在bytesIs中base64编码:)你只需要转换回字节数组
public class GetImagesController : Controller
{
    private readonly IWebHostEnvironment _host;

    public GetImagesController(IWebHostEnvironment host)
    {
        _host = host;
    }

    [HttpGet("{images}")]
    public async Task<List<byte[]>> Get([FromQuery]string images)
    {
        List<byte[]> imageBytes = new List<byte[]>();
        String[] strArray = images.Split(',');
        for (int i = 0; i < strArray.Length; i++)
        {
            String filePath = Path.Combine(_host.ContentRootPath, "images", strArray[i]+".jpg");
            byte[] bytes =  System.IO.File.ReadAllBytes(filePath);
            imageBytes.Add(bytes);

        }
        return imageBytes;
    }
}