2

I am newbie to angularJS. I want to submit a form with angularjs.

I am using the following code :

var app = angular.module('zippyModule', []);

app.controller('Ctrl3', function($scope, $http, $location,$compile) {

    $scope.submit = function(){
        alert($scope.filename);
    }

});

and in form I am using :

  <div ng-show="show">
           <form ng-submit="submit()"  enctype="multipart/form-data">
           <input type="file" ng-model="filename" name="filename" />
           <br/>
           <input type="submit" class="btn btn-primary" name="upload" value="Upload">
           </form>
           </div>

When I am submitting the form then it is alerting undefined. How can I print the value of the file field after submitting the form ?

Rohitashv Singhal
  • 4,517
  • 13
  • 57
  • 105

1 Answers1

1

Below is the working example of file upload with angularjs

http://jsfiddle.net/danielzen/utp7j/

check the set file function added this in your controller

 scope.setFiles = function(element) {
    scope.$apply(function(scope) {
      console.log('files:', element.files);
      // Turn the FileList object into an Array
        scope.files = []
        for (var i = 0; i < element.files.length; i++) {
          scope.files.push(element.files[i])
        }
      scope.progressVisible = false
      });
    };

and added this line in your template

 <input type="file" ng-model-instant id="fileToUpload" multiple onchange="angular.element(this).scope().setFiles(this)" />
JQuery Guru
  • 6,943
  • 1
  • 33
  • 40