AngularJS Uploading An Image With ng-upload

前端 未结 4 1015
清酒与你
清酒与你 2020-12-02 08:31

I am trying to upload a file in AngularJS using ng-upload but I am running into issues. My html looks like this:

相关标签:
4条回答
  • 2020-12-02 09:00

    In my case above mentioned methods work fine with php but when i try to upload files with these methods in node.js then i have some problem. So instead of using $http({..,..,...}) use the normal jquery ajax.

    For select file use this

    <input type="file" name="file" onchange="angular.element(this).scope().uploadFile(this)"/>
    

    And in controller

    $scope.uploadFile = function(element) {   
    var data = new FormData();
    data.append('file', $(element)[0].files[0]);
    jQuery.ajax({
          url: 'brand/upload',
          type:'post',
          data: data,
          contentType: false,
          processData: false,
          success: function(response) {
          console.log(response);
          },
          error: function(jqXHR, textStatus, errorMessage) {
          alert('Error uploading: ' + errorMessage);
          }
     });   
    };
    
    0 讨论(0)
  • 2020-12-02 09:06

    There's little-no documentation on angular for uploading files. A lot of solutions require custom directives other dependencies (jquery in primis... just to upload a file...). After many tries I've found this with just angularjs (tested on v.1.0.6)

    html

    <input type="file" name="file" onchange="angular.element(this).scope().uploadFile(this.files)"/>
    

    Angularjs (1.0.6) not support ng-model on "input-file" tags so you have to do it in a "native-way" that pass the all (eventually) selected files from the user.

    controller

    $scope.uploadFile = function(files) {
        var fd = new FormData();
        //Take the first selected file
        fd.append("file", files[0]);
    
        $http.post(uploadUrl, fd, {
            withCredentials: true,
            headers: {'Content-Type': undefined },
            transformRequest: angular.identity
        }).success( ...all right!... ).error( ..damn!... );
    
    };
    

    The cool part is the undefined content-type and the transformRequest: angular.identity that give at the $http the ability to choose the right "content-type" and manage the boundary needed when handling multipart data.

    0 讨论(0)
  • 2020-12-02 09:09

    You can try ng-file-upload angularjs plugin (instead of ng-upload).

    It's fairly easy to setup and deal with angularjs specifics. It also supports progress, cancel, drag and drop and is cross browser.

    html

    <!-- Note: MUST BE PLACED BEFORE angular.js-->
    <script src="ng-file-upload-shim.min.js"></script> 
    <script src="angular.min.js"></script>
    <script src="ng-file-upload.min.js"></script> 
    
    <div ng-controller="MyCtrl">
      <input type="file" ngf-select="onFileSelect($files)" multiple>
    </div>
    

    JS:

    //inject angular file upload directives and service.
    angular.module('myApp', ['ngFileUpload']);
    
    var MyCtrl = [ '$scope', '$upload', function($scope, $upload) {
      $scope.onFileSelect = function($files) {
        //$files: an array of files selected, each file has name, size, and type.
        for (var i = 0; i < $files.length; i++) {
          var file = $files[i];
          $scope.upload = $upload.upload({
            url: 'server/upload/url', //upload.php script, node.js route, or servlet url
            data: {myObj: $scope.myModelObj},
            file: file,
          }).progress(function(evt) {
            console.log('percent: ' + parseInt(100.0 * evt.loaded / evt.total));
          }).then(function(response) {
            var data = response.data;
            // file is uploaded successfully
            console.log(data);
          });
        }
      };
    }];
    
    0 讨论(0)
  • 2020-12-02 09:18
            var app = angular.module('plunkr', [])
        app.controller('UploadController', function($scope, fileReader) {
            $scope.imageSrc = "";
    
            $scope.$on("fileProgress", function(e, progress) {
            $scope.progress = progress.loaded / progress.total;
            });
        });
    
    
    
    
        app.directive("ngFileSelect", function(fileReader, $timeout) {
            return {
            scope: {
                ngModel: '='
            },
            link: function($scope, el) {
                function getFile(file) {
                fileReader.readAsDataUrl(file, $scope)
                    .then(function(result) {
                    $timeout(function() {
                        $scope.ngModel = result;
                    });
                    });
                }
    
                el.bind("change", function(e) {
                var file = (e.srcElement || e.target).files[0];
                getFile(file);
                });
            }
            };
        });
    
        app.factory("fileReader", function($q, $log) {
        var onLoad = function(reader, deferred, scope) {
            return function() {
            scope.$apply(function() {
                deferred.resolve(reader.result);
            });
            };
        };
    
        var onError = function(reader, deferred, scope) {
            return function() {
            scope.$apply(function() {
                deferred.reject(reader.result);
            });
            };
        };
    
        var onProgress = function(reader, scope) {
            return function(event) {
            scope.$broadcast("fileProgress", {
                total: event.total,
                loaded: event.loaded
            });
            };
        };
    
        var getReader = function(deferred, scope) {
            var reader = new FileReader();
            reader.onload = onLoad(reader, deferred, scope);
            reader.onerror = onError(reader, deferred, scope);
            reader.onprogress = onProgress(reader, scope);
            return reader;
        };
    
        var readAsDataURL = function(file, scope) {
            var deferred = $q.defer();
    
            var reader = getReader(deferred, scope);
            reader.readAsDataURL(file);
    
            return deferred.promise;
        };
    
        return {
            readAsDataUrl: readAsDataURL
        };
        });
    
    
    
        *************** CSS ****************
    
        img{width:200px; height:200px;}
    
        ************** HTML ****************
    
        <div ng-app="app">
        <div ng-controller="UploadController ">
            <form>
            <input type="file" ng-file-select="onFileSelect($files)" ng-model="imageSrc">
                    <input type="file" ng-file-select="onFileSelect($files)" ng-model="imageSrc2">
            <!--  <input type="file" ng-file-select="onFileSelect($files)" multiple> -->
            </form>
    
            <img ng-src="{{imageSrc}}" />
        <img ng-src="{{imageSrc2}}" />
    
        </div>
        </div>
    
    0 讨论(0)
提交回复
热议问题