A
A
Alexey2015-05-26 17:35:28
Angular
Alexey, 2015-05-26 17:35:28

How to get and send an image in Angular JS?

Faced such a problem.
There is a URL where I need to get the image. With the help of "$http.get" the request occurs correctly, but in the response I get a binary. The question is how to process it so that it can be inserted into the page?

$http.get('https://exampleurl/images/' + $scope.element[i].id)
                            .success(function (response) {
          // Здесь response будет такого вида ""����JFIF``��;CREATOR gd-jpeg v1.0 (using IJG JPEG v80)"
                            });

RESOLVED!
<img data-ng-src="https://some.url/images/{{el.id}}" alt="{{el.name}}" err-src="http://www.someurl.com/Aug/no-cat-food.jpg"/>

err-src directive:
app.directive('errSrc', function() {
        return {
            link: function(scope, element, attrs) {

                scope.$watch(function() {
                    return attrs['ngSrc'];
                }, function (value) {
                    if (!value) {
                        element.attr('src', attrs.errSrc);
                    }
                });

                element.bind('error', function() {
                    element.attr('src', attrs.errSrc);
                });
            }
        }
    });

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Maxim Dunayevsky, 2015-05-26
@azovl

Why do you need this addiction? What prevents you from doing ng-src , and getting a link via GET, and not the image itself? This doesn't make any sense.
I recommend this plugin for uploading: angular-file-upload
I tried a few more pieces of different ones, but it’s not the same, they didn’t even come close in terms of functionality.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question