File Upload using AngularJS

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

Here is my HTML form:

29条回答
  •  别跟我提以往
    2020-11-21 08:21

    The easiest is to use HTML5 API, namely FileReader

    HTML is pretty straightforward:

    
    
    

    In your controller define 'add' method:

    $scope.add = function() {
        var f = document.getElementById('file').files[0],
            r = new FileReader();
    
        r.onloadend = function(e) {
          var data = e.target.result;
          //send your binary data via $http or $resource or do anything else with it
        }
    
        r.readAsBinaryString(f);
    }
    

    Browser Compatibility

    Desktop Browsers

    Edge 12, Firefox(Gecko) 3.6(1.9.2), Chrome 7, Opera* 12.02, Safari 6.0.2

    Mobile Browsers

    Firefox(Gecko) 32, Chrome 3, Opera* 11.5, Safari 6.1

    Note : readAsBinaryString() method is deprecated and readAsArrayBuffer() should be used instead.

提交回复
热议问题