2015-07-12 72 views
2

我試圖讓圖像預覽文件後,選擇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);工作正常,但它是工作的唯一一個

回答

1

您的數據-NG-SRC應該"data:image/png;base64,iVBORw0..." 前綴您可以在控制檯日誌中找到一個更好的描述here

+0

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

+0

你可以爲這個創建一個plunker並且複製你的base64字符串的圖像嗎? –