Load image from C# Byte array and place image in html tag using AngularJS

后端 未结 2 1810
暗喜
暗喜 2021-01-17 06:09

I\'m having an Image in the form of Byte Array, from that I\'m converting Byte Array from the following C# method

public HttpResponseMessage ReturnBytes(byte         


        
相关标签:
2条回答
  • 2021-01-17 06:52

    Send your image not as a byte array, but Convert.ToBase64String the byte array and send as text. Then your code should work.

    0 讨论(0)
  • 2021-01-17 07:00

    Instead of using the HttpResponseMessage() method, just convert the Byte Array to Base64String and send it back to the client as a Response of type String.

    The C# Source Code:

    [HttpPost]
    public string GetCalculatorResults()
    {
        byte[] imgArr = GetImageFromDataBase();
    
        // Here we are converting the byte array to Base64String
        return Convert.ToBase64String(imgArr)
    }
    

    The HTML and AngularJS Source Code Should be

    <div ng-app="myApp" ng-controller="myCtrl">
    
        <img ng-src="data:image/jpeg;base64,{{ imageSrc  }}" />
    
    </div>
    
    <script>
    var app = angular.module('myApp', []);
    app.controller('myCtrl', function($scope, $http) {
        $scope.imageSrc = "";
        $http.get("someurl")
        .then(function(response) {
            $scope.imageSrc = response.data;
        });
    });
    </script>
    

    I tested the above code, its working perfectly.

    0 讨论(0)
提交回复
热议问题