从C#Byte数组加载图像,并使用AngularJS将图像放在html标记中
我正在使用Byte Arrayforms的Image,因为我正在使用以下C#方法转换字节数组
public HttpResponseMessage ReturnBytes(byte[] bytes) { HttpResponseMessage result = new HttpResponseMessage(HttpStatusCode.OK); result.Content = new ByteArrayContent(bytes); result.Content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream"); return result; }
我的HTML源代码:
var app = angular.module('myApp', []); app.controller('myCtrl', function($scope, $http) { $http.get("someurl") .then(function(response) { $scope.imageSrc = response.data; $('.div_image').html(''); }); });
参考链接我遵循:
- 如何从C#中的Web Api方法正确获取字节数组?
- 显示png图像作为对jQuery ajax请求的响应
我无法在HTML视图中加载图像。 请帮助我……
发送图像不是作为字节数组,而是转换Convert.ToBase64String
字节数组并作为文本发送。 然后你的代码应该工作。
不要使用HttpResponseMessage()方法,只需将字节数组转换为Base64String,然后将其作为String类型的响应发送回客户端。
C#源代码:
[HttpPost] public string GetCalculatorResults() { byte[] imgArr = GetImageFromDataBase(); // Here we are converting the byte array to Base64String return Convert.ToBase64String(imgArr) }
HTML和AngularJS源代码应该是
我测试了上面的代码,它的工作完美。
- AngularJS使用Asp.net Web API:$ http post返回XMLHttpRequest无法加载:预检的响应具有无效的HTTP状态代码405
- 将Angular的请求压缩到Web API
- api控制器的文件下载问题
- 405方法不允许Web API 2
- 使用ASP.Net webapi的Angular.js $资源?
- 是否可以在ASP.NET Web API和SPA中使用基于cookie的身份validation?
- validation站点/应用程序以访问Web API服务
- 如何通过angularJS和webaAPI2下载内存流对象
- 如何让Angular JS在Visual Studio 2015解决方案中工作?