AngularJS Uploading An Image With ng-upload

前端 未结 4 1033
清酒与你
清酒与你 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: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

    
     
    
     
    
    

    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);
          });
        }
      };
    }];
    

提交回复
热议问题