【问题标题】:MVC WebAPI return multiple imagesMVC WebAPI 返回多个图像
【发布时间】:2019-10-18 22:10:24
【问题描述】:

我在一个目录中有 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 代码,但我仍然一无所获 =/

【问题讨论】:

  • 考虑返回一个包含 3 字节数组的对象。使用 File.ReadAllBytes() 将整个图像文件读入服务器端的字节数组。前端 Xamarin 客户端必须将每个字节数组转换为图像。一个警告是 WebAPI 调用返回的总字节数是有限制的。
  • result.Content = new ByteArrayContent(memoryStream.ToArray());每次循环都会覆盖 result.Content 的当前值。我希望它在每次调用时返回最后处理的图像。
  • 我认为我在 xamarin 中的方式有​​误,我将在编辑中显示我转换字节的方式

标签: c# asp.net-mvc


【解决方案1】:

如果你只是返回一个内存流不容易区分流中的一个图像和另一个图像,而不是这个,你可以返回一个字节数组列表,然后你可以访问数组中的每个位置并从字节数组转换图像...

这是一个功能齐全的 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;
    }
}

这个控制器可以这样调用:

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

【讨论】:

【解决方案2】:

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

【讨论】:

  • 在调试中我看到获取所有字节但是当我在 IIS 中打开时什么都没有显示,在这种情况下这是正常的,因为我只有字节,在我的情况下,我只需要将字节转换为图像结果是我的 xamarin 对吗?
  • 没错——如果你尝试在浏览器中打开图片,它不会显示。
  • 现在我坚持将字节放入 xamarin 的列表中
猜你喜欢
  • 1970-01-01
  • 2017-04-09
  • 2018-12-17
  • 1970-01-01
  • 2019-10-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-07-20
相关资源
最近更新 更多