File Upload using AngularJS

前端 未结 29 2406
野趣味
野趣味 2020-11-21 07:24

Here is my HTML form:

29条回答
  •  耶瑟儿~
    2020-11-21 08:05

    Above accepted answer is not browser compatible. If some one has compatibility issue try this.

    Fiddle

    View Code

     

    {{data}}

    Controller code

    var myApp = angular.module('myApp',[]);
    
    function MyCtrl($scope) {
        $scope.data = 'none';    
        $scope.add = function(){
          var f = document.getElementById('file').files[0],
              r = new FileReader();
          r.onloadend = function(e){        
              var binary = "";
    var bytes = new Uint8Array(e.target.result);
    var length = bytes.byteLength;
    
    for (var i = 0; i < length; i++) 
    {
        binary += String.fromCharCode(bytes[i]);
    }
    
    $scope.data = (binary).toString();
    
              alert($scope.data);
          }
          r.readAsArrayBuffer(f);
        }
    }
    

提交回复
热议问题