【问题标题】:ASP.Net web api return image and text togetherASP.Net web api一起返回图像和文本
【发布时间】:2020-04-19 03:31:07
【问题描述】:

我正在寻找在同一 HTTP 获取消息中返回文本和图像的示例。我已经看到返回文件并看到复杂的对象,但在同一个响应中没有看到任何同时包含文本和图像的对象。

【问题讨论】:

    标签: asp.net asp.net-web-api webapi


    【解决方案1】:

    请试试这个代码 您可以通过 GET APi 调用获取图像名称、文本和图像路径

    public byte[] GetImage(int componentId)
    {
                using (var dashboardService = new DashboardService())
                {
                    var component = dashboardService.GetImage(componentId);
                    var context = HttpContext.Current;
                    string filePath = context.Server.MapPath("~/Images/" + component.ImageName);
                    context.Response.ContentType = "image/jpeg";
                    using (FileStream fileStream = new FileStream(filePath, FileMode.Open))
                    {
                        using (var memoryStream = new MemoryStream())
                        {
                            fileStream.CopyTo(memoryStream);
                            Bitmap image = new Bitmap(1, 1);
                            image.Save(memoryStream, ImageFormat.Jpeg);
    
                            byte[] byteImage = memoryStream.ToArray();
                            return byteImage;
                        }
                    }
                }
    }
    

    【讨论】:

    • 谢谢。我今天会试试这个。
    猜你喜欢
    • 2014-03-02
    • 2017-05-22
    • 1970-01-01
    • 2018-07-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-11
    • 2016-04-23
    相关资源
    最近更新 更多