Sending an image to the server after displaying it in the UI in Angular

后端 未结 1 616
南笙
南笙 2021-01-22 04:32

I\'m getting an image upload, or from the camera in an Angular app:


相关标签:
1条回答
  • 2021-01-22 05:05

    You could use Daniel Afarid's Angular File Upload

    Controller

    $scope.selectedFiles = [];
    $scope.dataUrls = [];
    $scope.model = 'test model';
    
    $scope.onImageSelect = function($files) {
    
        $scope.selectedFiles['image'] = $files[0];
    
    
        var $file = $files[0];
        if (window.FileReader && $file.type.indexOf('image') > -1) {
            var fileReader = new FileReader();
            fileReader.readAsDataURL($files[0]);
    
            fileReader.onload = function(e) {
            $timeout(function() {
            $scope.dataUrls['image'] = e.target.result;
                });
            }
        }    
    
    }
    
    $scope.save_image =  function(){
    
            $upload.upload({
                      data:$scope.model,
                      url: 'the/url',
                      file: $scope.selectedFiles['image']
    
            }).then(//success and error callbacks);
    
    
        }
    

    View

    <form enctype="multipart/form-data" class="form-horizontal">
        <input type="file" ng-file-select="onImageSelect($files)">
        <br />
        <img ng-show="dataUrls['image']" ng-src="{{dataUrls['image']}}">
        <button ng-click="save_image()"></button>
    </form>
    
    0 讨论(0)
提交回复
热议问题