How to create directive for read file in angular

Posted By : Murari Kumar | 30-Dec-2016

In this blog, I am going to tell you that how to create directive for read file from html in angular js.

If I want to upload a file in our project then we need to read file from form after then we need to upload file.

There are two ways to select file to use :-

1.<input type = 'file'>

2.Drag and down a file into broser.

This directive is used for the file input.

For read file,a directive is given below:-

angular.module('urbanApp').directive("fileread", [function () {


       return {
           scope: {
               fileread: "="
           },
           link: function (scope, element, attributes) {
               element.bind("change", function (changeEvent) {
                   scope.$apply(function () {
                       scope.fileread = changeEvent.target.files[0];

                       // or all selected files:
                       // scope.fileread = changeEvent.target.files;
                   });

               });
           }
       }
   }])
 

I hope this blog will help you.

THANKS

About Author

Author Image
Murari Kumar

Murari is a bright Web developer. He has expertise in Java.

Request for Proposal

Name is required

Comment is required

Sending message..