0

I would like to have image preview upload with trigger another element, so that I have put together in directives.

https://stackoverflow.com/a/25674293

AngularJS Image Preview before Upload showing previous image selected

it is so confusing to me.

How can I make directives image preview uploading by clicking image? I try it put together but it fails,

https://jsfiddle.net/tictac18g/83m659zL/4/

    .directive('fileButton', function ($parse) {
    return {
        restrict: 'AE',
        link: function (scope, element, attrs) {

            scope.setimage = function () {
                var file = scope.myFile;
                var reader = new FileReader();
                reader.readAsDataURL(file);
                reader.onload = function (e) {
                    scope.$apply(function () {
                        scope.ImageSrc = e.target.result;
                    })
                }
            }

            var model = $parse(attrs.fileModel);
            var modelSetter = model.assign;

            element.bind('change', function (e) {
                scope.$apply(function (e) {
                    modelSetter(scope, element[0].files[0]);
                });
                scope.setimage();
            });
        },

        compile: function (element) {
            var fileInput = angular.element('<input type="file" file-model="myFile" multiple />')
            fileInput.css({
                position: 'absolute',
                top: 0,
                left: 0,
                'z-index': '2',
                width: '100%',
                height: '100%',
                opacity: '0',
                cursor: 'pointer'
            });
            element.append(fileInput)
            return this.link;
        }

    }
})

<div ng-app="myApp" ng-controller="myCtrl">
    <div ng-init="ImageSrc ='https://www.google.com/images/srpr/logo3w.png'" file-button>
        <img width="200" ng-src="{{ImageSrc}}" />
    </div>
</div>

Thanks in advance.

update: I have put two separate directives, it seems work, however I can't can't figure out to make one directive.

here is my result, https://jsfiddle.net/tictac18g/4y0seej9/

but the result doesn't match what I'm looking for, because the size of uploaded image couldn't be restraint.

and it doesn't quite the same result as the jquery, http://jsfiddle.net/rodolphobrock/q8f33f8L/

would you give me some advise, it can't be achieve the same features in angularjs?

update 2: I can't get it work with native directives, so I have applied it with JQuery plugins.

my result is that. https://fiddle.jshell.net/tictac18g/83m659zL/

2
  • Hi, I am also trying to preview images before uploading to database. I came across you post, and it's really helpful, but you are clicking on image to change the existing image with selected one. I want to select multiple mages and show them before uploading to database.How can I do that, please help.. Commented Sep 28, 2017 at 7:25
  • I have been away the project. github.com/blueimp/jQuery-File-Upload It is Jquery plugin and support multiple image on AngularJS. I sorry to give you helpful much. Commented Oct 2, 2017 at 13:12

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.