首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >API到WEB的图像显示结果

API到WEB的图像显示结果
EN

Stack Overflow用户
提问于 2019-11-25 03:04:31
回答 1查看 54关注 0票数 0

从字节到图像的检索和图像转换非常有效。

API代码:

代码语言:javascript
复制
[HttpGet]
        public HttpResponseMessage MemberImage(string employeeId)
        {
            IMemberProcedures storedProcedure = new StoredProcedure();
            IValidation validation = new CommonRepository();
            RequestModel request = SQL.Read(out List<MemberDetail> members, storedProcedure.SAMPLESTOREDPROCEDURE("::1", employeeId));
            byte[] imgData = members[0].Picture;
            MemoryStream ms = new MemoryStream(imgData);
            HttpResponseMessage response = new HttpResponseMessage(HttpStatusCode.OK);
            response.Content = new StreamContent(ms);
            response.Content.Headers.ContentType = new
            MediaTypeHeaderValue("image/png");
            return response;
        }

在邮递员中尝试这样做会返回一个完全转换的图像。

现在我想在我的网页上显示这个图像,这就是我尝试过的;

网络代码:

代码语言:javascript
复制
[HttpGet]
public ActionResult MemberImage(string id)
        {
            IGetInterface Ip = new IpHelper();
            HttpResponseMessage image = API.GetResponse($"api/QMS/MemberImage?employeeId={id}");
            var foo = image.Content.ReadAsStringAsync().Result;
            return File(Enumerable.Range(0, foo.Length)
                             .Where(x => x % 2 == 0)
                             .Select(x => Convert.ToByte(foo.Substring(x, 2), 16))
                             .ToArray(), "image/png");
        }

这引发了一个不受支持的媒体错误,我认为我做的不是正确的方式显示图像从网络控制器到我的观点。

还有其他方法将API的图像结果显示到上吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-11-25 03:11:45

最后,经过几次重试,我使用ReadAsByteArrayAsync()使其工作。

代码语言:javascript
复制
[HttpGet]
        public ActionResult MemberImage(string id)
        {
            IGetInterface Ip = new IpHelper();
            HttpResponseMessage image = API.GetResponse($"api/QMS/MemberImage?employeeId={id}");
            byte[] foo = image.Content.ReadAsByteArrayAsync().Result;
            return File(foo, "image/png");

        }

通过这样做,将其显示为视图;

<img src='@Url.Action("MemberImage", new { id = Model.Member.EmployeeNumber })'>

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59024722

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档