Preview Image before uploading Angularjs

前端 未结 4 619
伪装坚强ぢ
伪装坚强ぢ 2020-11-28 04:10

Hi I was wondering if there was a way to preview images before I upload them using angularjs? I am using the this library. https://github.com/danialfarid/angular-file-uploa

相关标签:
4条回答
  • 2020-11-28 04:39

    OdeToCode posted great service for this stuff. So with this simple directive you can easily preview and even see the progress bar:

    .directive("ngFileSelect",function(){    
      return {
        link: function($scope,el){          
          el.bind("change", function(e){          
            $scope.file = (e.srcElement || e.target).files[0];
            $scope.getFile();
          });          
        }        
      }
    

    It is working in all modern browsers!

    Example: http://plnkr.co/edit/y5n16v?p=preview

    0 讨论(0)
  • 2020-11-28 04:54

    JavaScript

     $scope.setFile = function(element) {
      $scope.currentFile = element.files[0];
       var reader = new FileReader();
    
      reader.onload = function(event) {
        $scope.image_source = event.target.result
        $scope.$apply()
    
      }
      // when the file is read it triggers the onload event above.
      reader.readAsDataURL(element.files[0]);
    }
    

    Html

    <img ng-src="{{image_source}}">
    <input type="file" id="trigger" class="ng-hide" onchange="angular.element(this).scope().setFile(this)" accept="image/*">
    

    This worked for me.

    0 讨论(0)
  • 2020-11-28 04:55

    See the Image Upload Widget from the Jasney extension of Bootstrap v3

    0 讨论(0)
  • 2020-11-28 04:56
      // start Picture Preview    
        $scope.imageUpload = function (event) {
            var files = event.target.files;
    
            for (var i = 0; i < files.length; i++) {
                var file = files[i];
                var reader = new FileReader();
                reader.onload = $scope.imageIsLoaded;
                reader.readAsDataURL(file);
            }
        }
    
        $scope.imageIsLoaded = function (e) {
            $scope.$apply(function () {
                $scope.img = e.target.result;            
            });
        }
    
    
    <input type='file' ng-model-instant onchange="angular.element(this).scope().imageUpload(event)" />
    <img class="thumb" ng-src="{{img}}" />
    
    0 讨论(0)
提交回复
热议问题