显示的base64图像

问题描述:

我试图让图像预览文件后,选择AngularJS显示的base64图像

它的HTML我使用选择文件,并与NG重复显示它

<input type="file" file-input="files" multiple /> 
<li ng-repeat="file in files">[[file.name]] <img data-ng-src="[[todata(file)]]"></li> 

和我的角码

app.directive('fileInput', ['$parse', function($parse){ 
       return { 
        restrict:'A', 
        link:function(scope,elm,attrs){ 
         elm.bind('change', function(){ 


          $parse(attrs.fileInput) 
          .assign(scope,elm[0].files) 
          scope.$apply() 

         }) 
        } 
       } 

      }]) 
.controller('fileUploader', ['$scope', '$http', 
         function ($scope, $http) { 

          $scope.todata = function(file) { 
          var oFReader = new FileReader(); 
          oFReader.readAsDataURL(file); 

          oFReader.onload = function(oFREvent) { 
           return oFREvent.target.result 
          }; 
          } 
       }]); 

其显示图像的名字,但不是图像本身,在控制台记录的base64 todata功能转换的图像,但它的HTML显示什么..

如何显示图像以进行预览?

编辑

console.log(oFREvent.target.result);工作正常,但它是工作的唯一一个

您的数据-NG-SRC应该"data:image/png;base64,iVBORw0..." 前缀您可以在控制台日志中找到一个更好的描述here

+0

用'data:image/jpeg; base64,/ 9j /'认为它不需要 –

+0

你可以为这个创建一个plunker并且复制你的base64字符串的图像吗? –