从Web.API返回图像并使用img标记显示

时间:2015-09-06 03:19:36

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

我有一个Web API,它返回一个图像,从存储在数据库中的图像中检索字节数组为varbinary(max)。但图像没有显示出来。我做错了什么?

以下是Web API

        [Route("service/products/{id?}/thumbnail")]
        public HttpResponseMessage GetPiThumbnail(long id)
        {
            if (id == 0) return new HttpResponseMessage(HttpStatusCode.NotFound);

            var data = _productsRepository.Get(a => a.ProductId == id).SingleOrDefault().ThumbnailImage;

            if (data == null) return new HttpResponseMessage(HttpStatusCode.NotFound);

            HttpResponseMessage response = new HttpResponseMessage(HttpStatusCode.OK);
            response.Content = new ByteArrayContent(data);
            response.Content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");

            return response;
        }

以下是ASP.NET MVC应用中razor文件的片段,用于显示上述Web API返回的图像 -

      <div><img src="../service/products/378728/thumbnail" /></div> 

0 个答案:

没有答案