ng-model for `<input type=“file”/>` (with directive DEMO)

后端 未结 12 2258
北恋
北恋 2020-11-21 06:40

I tried to use ng-model on input tag with type file:


But after selecting a file, in

12条回答
  •  无人及你
    2020-11-21 07:09

    I had to do same on multiple input, so i updated @Endy Tjahjono method. It returns an array containing all readed files.

      .directive("fileread", function () {
        return {
          scope: {
            fileread: "="
          },
          link: function (scope, element, attributes) {
            element.bind("change", function (changeEvent) {
              var readers = [] ,
                  files = changeEvent.target.files ,
                  datas = [] ;
              for ( var i = 0 ; i < files.length ; i++ ) {
                readers[ i ] = new FileReader();
                readers[ i ].onload = function (loadEvent) {
                  datas.push( loadEvent.target.result );
                  if ( datas.length === files.length ){
                    scope.$apply(function () {
                      scope.fileread = datas;
                    });
                  }
                }
                readers[ i ].readAsDataURL( files[i] );
              }
            });
    
          }
        }
      });
    

提交回复
热议问题