How to read a file in AngularJS?

后端 未结 3 625
时光取名叫无心
时光取名叫无心 2020-12-04 15:49

Is it possible to read files in AngularJS? I want to place the file into an HTML5 canvas to crop.

I was thinking of using a directive? This is the javascript code I

3条回答
  •  时光说笑
    2020-12-04 16:48

    Angular File reader.

    link: function(scope, element, attrs) {
                element.on('change', function(e) {
                    var reader = new FileReader();
                    reader.onload = function(e) {
                        scope.$apply(function() {
                           scope.onReadFile({$content:e.target.result});
                        });
                    };
                    reader.readAsText((e.srcElement || e.target).files[0]);
                });
            }
    

    Live example : Live Run On Plunker

提交回复
热议问题