问题
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.
They're both located in my HomeController.cs
.
API:
Here's the API call:
[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");
}
Razor:
Here's the Razor helper:
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");
}
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:
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:
Here's the API call preview
in Network tab:
Razor:
Here's the Razor Helper call preview
in Network tab:
API:
Here's the API call headers
in Network tab:
Razor:
Here's the Razor Helper call headers
in Network tab:
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?
回答1:
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>
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));
}
}
}
来源:https://stackoverflow.com/questions/45066586/how-to-set-returned-image-data-as-ng-src-of-image-in-angularjs