MVC WebAPI返回多个图像

问题描述 投票:2回答:2

我在目录中有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;

现在我得到字节,我现在编辑一点代码

        byte[] imageAsBytes = client.GetByteArrayAsync(url).Result; 

        MemoryStream stream1 = new MemoryStream(imageAsBytes);
        img.Source = ImageSource.FromStream(() => { return stream1; });

这是我的xamarin代码以获取图像,但是我仍然什么也没得到= /

c# asp.net-mvc
2个回答
0
投票

您需要在响应中指定一些内容来界定每个图像的开始和结束位置。作为基本解决方案,您可以将图像长度写为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;
}

0
投票

如果仅返回一个内存流并不容易区分流中的一个图像,而是可以返回一个字节数组列表,则可以访问该数组中的每个位置并将字节数组转换为图片...

这里是功能齐全的dotnet核心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;
    }
}

此控制器可以这样称呼:

https://localhost:44386/getImages?images=P1,P2,P3

鉴于您在ContentRooPath下有一个名为images的文件夹,其中包含文件P1.jpg,P2.jpg和P3.jpg。

https://docs.microsoft.com/en-us/aspnet/core/fundamentals/host/web-host?view=aspnetcore-3.0

© www.soinside.com 2019 - 2024. All rights reserved.