如何在AngularJS中将返回的图像数据设置为图像的ng-src [英] How to set returned Image Data as ng-src of image in AngularJS
问题描述
目前,我有两种ASP.Net Core方法-一种与API调用相关,另一种与客户端Razor帮助程序相关.
I've got two ASP.Net Core methods at the moment - one tied to an API call, and one tied to a client-side Razor helper.
它们都位于我的 HomeController.cs
中.
API:
这是API调用:
[HttpGet("api/GetImage")]
public async Task<ActionResult> ImageFromPath()
{
RestClient client = new RestClient("http://IPADDRESS/cgi-bin/snapshot.cgi?channel=0");
RestRequest request = new RestRequest(Method.GET);
request.AddHeader("cache-control", "no-cache");
request.AddHeader("authorization", "Basic aYu7GI");
TaskCompletionSource<IRestResponse> taskCompletion = new TaskCompletionSource<IRestResponse>();
RestRequestAsyncHandle handle = client.ExecuteAsync(request, r => taskCompletion.SetResult(r));
RestResponse response = (RestResponse)(await taskCompletion.Task);
return File(response.RawBytes, "image/png");
}
剃刀:
这是剃刀的助手:
public async Task<ActionResult> ImageFromPath()
{
RestClient client = new RestClient("http://IPADDRESS/cgi-bin/snapshot.cgi?channel=0");
RestRequest request = new RestRequest(Method.GET);
request.AddHeader("cache-control", "no-cache");
request.AddHeader("authorization", "Basic aYu7GI");
TaskCompletionSource<IRestResponse> taskCompletion = new TaskCompletionSource<IRestResponse>();
RestRequestAsyncHandle handle = client.ExecuteAsync(request, r => taskCompletion.SetResult(r));
RestResponse response = (RestResponse)(await taskCompletion.Task);
return File(response.RawBytes, "image/png");
}
如您所见,上面的方法主体完全相同.一个只是带有 HttpGet
批注.
As you can see, the method bodies above are exactly the same. One simply has the HttpGet
annotation.
但是,当与客户端处理程序配对时,它们会产生不同的结果.
Yet, when paired with their client-side handlers, they produce different results.
API:
用于API调用的客户端处理程序:
Client-side handler for API call:
<img ng-src="{{imageSrc}}">
$http.get('/api/GetImage', { headers: { 'Accept': 'image/webp,image/apng,image/*,*/*;q=0.8' } }).then(function (response) {
$scope.imageSrc = response.data;
});
剃刀:
Razor助手的客户端处理程序:
Client-side handler for Razor helper:
<img src="@Url.Action("ImageFromPath")" id="img2" />
结果显示在这里:
Here's where the results show:
API:
以下是网络"标签中的API调用 preview
:
Here's the API call preview
in Network tab:
剃刀:
以下是网络"标签中的剃刀助手"调用 preview
:
Here's the Razor Helper call preview
in Network tab:
API:
这是网络"标签中的API调用 headers
:
Here's the API call headers
in Network tab:
剃刀:
这是网络"标签中的剃刀助手"呼叫 headers
:
Here's the Razor Helper call headers
in Network tab:
API:
为什么Razor帮助程序实际上产生/返回图像,而API调用却没有?像Razor助手一样,如何使API调用返回图像?
Why does the Razor helper actually produce/return an image, yet the API call doesn't? How could I make the API call return an image just like the Razor helper does?
推荐答案
您只需要几件事.首先,您不应直接在Angular Controller中访问DOM.
You need few things. First, you should not access DOM directly inside Angular Controller.
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.5.3/angular.js"></script>
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.5.3/angular-animate.js"></script>
<script src="http://angular-ui.github.io/bootstrap/ui-bootstrap-tpls-1.3.2.js"></script>
<div ng-app="app">
<my-content></my-content>
</div>
<script type="text/javascript">
angular.module('app', []);
angular.module('app')
.component('myContent', {
template: '<h1>{{$ctrl.title}}</h1><img src="{{$ctrl.imageSrc}}" />',
controller: function ($http) {
var self = this;
self.title = "Title";
// Just to verify image work
//self.imageSrc = "https://i.stack.imgur.com/hM8Ah.jpg?s=48&g=1";
$http.get('/Home/ImageFromPath').then(function (response) {
console.log(response.data);
self.imageSrc = response.data;
});
}
});
</script>
第二,您需要返回基于base64编码的字符串.
Second, you will need to return based64 encoded string.
using System;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using RestSharp;
namespace DemoWebCore.Controllers
{
public class HomeController : Controller
{
public async Task<IActionResult> ImageFromPath()
{
RestClient client = new RestClient("https://i.stack.imgur.com/hM8Ah.jpg?s=48&g=1");
RestRequest request = new RestRequest(Method.GET);
request.AddHeader("cache-control", "no-cache");
request.AddHeader("authorization", "Basic aYu7GI");
TaskCompletionSource<IRestResponse> taskCompletion = new TaskCompletionSource<IRestResponse>();
RestRequestAsyncHandle handle = client.ExecuteAsync(request, r => taskCompletion.SetResult(r));
RestResponse response = (RestResponse)await taskCompletion.Task;
return Ok("data:image/png;base64," + Convert.ToBase64String(response.RawBytes));
}
}
}
这篇关于如何在AngularJS中将返回的图像数据设置为图像的ng-src的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!