AngularJs: How to check for changes in file input fields?

前端 未结 15 1154
傲寒
傲寒 2020-11-22 09:06

I am new to angular. I am trying to read the uploaded file path from HTML \'file\' field whenever a \'change\' happens on this field. If i use \'onChange\' it works but when

相关标签:
15条回答
  • 2020-11-22 09:35

    I've expanded on @Stuart Axon's idea to add two-way binding for the file input (i.e. allow resetting the input by resetting the model value back to null):

    app.directive('bindFile', [function () {
        return {
            require: "ngModel",
            restrict: 'A',
            link: function ($scope, el, attrs, ngModel) {
                el.bind('change', function (event) {
                    ngModel.$setViewValue(event.target.files[0]);
                    $scope.$apply();
                });
    
                $scope.$watch(function () {
                    return ngModel.$viewValue;
                }, function (value) {
                    if (!value) {
                        el.val("");
                    }
                });
            }
        };
    }]);
    

    Demo

    0 讨论(0)
  • 2020-11-22 09:36

    The clean way is to write your own directive to bind to "change" event. Just to let you know IE9 does not support FormData so you cannot really get the file object from the change event.

    You can use ng-file-upload library which already supports IE with FileAPI polyfill and simplify the posting the file to the server. It uses a directive to achieve this.

    <script src="angular.min.js"></script>
    <script src="ng-file-upload.js"></script>
    
    <div ng-controller="MyCtrl">
      <input type="file" ngf-select="onFileSelect($files)" multiple>
    </div>
    

    JS:

    //inject angular file upload directive.
    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];
          Upload.upload({
            url: 'my/upload/url',
            data: {file: $file}
          }).then(function(data, status, headers, config) {
            // file is uploaded successfully
            console.log(data);
          }); 
        }
      }
    }];
    
    0 讨论(0)
  • 2020-11-22 09:38

    I made a small directive to listen for file input changes.

    View JSFiddle

    view.html:

    <input type="file" custom-on-change="uploadFile">
    

    controller.js:

    app.controller('myCtrl', function($scope){
        $scope.uploadFile = function(event){
            var files = event.target.files;
        };
    });     
    

    directive.js:

    app.directive('customOnChange', function() {
      return {
        restrict: 'A',
        link: function (scope, element, attrs) {
          var onChangeHandler = scope.$eval(attrs.customOnChange);
          element.on('change', onChangeHandler);
          element.on('$destroy', function() {
            element.off();
          });
    
        }
      };
    });
    
    0 讨论(0)
  • 2020-11-22 09:38

    Too complete solution base on:

    `onchange="angular.element(this).scope().UpLoadFile(this.files)"`
    

    A simple way to hide the input field and replace it with a image, here after a solution, that also require a hack on angular but that do the job [TriggerEvent does not work as expected]

    The solution:

    • place the input-field in display:none [the input field exist in the DOM but is not visible]
    • place your image right after On the image use nb-click() to activate a method

    When the image is clicked simulate a DOM action 'click' on the input field. Et voilà!

     var tmpl = '<input type="file" id="{{name}}-filein"' + 
                 'onchange="angular.element(this).scope().UpLoadFile(this.files)"' +
                 ' multiple accept="{{mime}}/*" style="display:none" placeholder="{{placeholder}}">'+
                 ' <img id="{{name}}-img" src="{{icon}}" ng-click="clicked()">' +
                 '';
       // Image was clicked let's simulate an input (file) click
       scope.inputElem = elem.find('input'); // find input in directive
       scope.clicked = function () {
             console.log ('Image clicked');
             scope.inputElem[0].click(); // Warning Angular TriggerEvent does not work!!!
        };
    
    0 讨论(0)
  • 2020-11-22 09:41

    No binding support for File Upload control

    https://github.com/angular/angular.js/issues/1375

    <div ng-controller="form-cntlr">
            <form>
                 <button ng-click="selectFile()">Upload Your File</button>
                 <input type="file" style="display:none" 
                    id="file" name='file' onchange="angular.element(this).scope().fileNameChanged(this)" />
            </form>  
        </div>
    

    instead of

     <input type="file" style="display:none" 
        id="file" name='file' ng-Change="fileNameChanged()" />
    

    can you try

    <input type="file" style="display:none" 
        id="file" name='file' onchange="angular.element(this).scope().fileNameChanged()" />
    

    Note: this requires the angular application to always be in debug mode. This will not work in production code if debug mode is disabled.

    and in your function changes instead of

    $scope.fileNameChanged = function() {
       alert("select file");
    }
    

    can you try

    $scope.fileNameChanged = function() {
      console.log("select file");
    }
    

    Below is one working example of file upload with drag drop file upload may be helpful http://jsfiddle.net/danielzen/utp7j/

    Angular File Upload Information

    URL for AngularJS File Upload in ASP.Net

    http://cgeers.com/2013/05/03/angularjs-file-upload/

    AngularJs native multi-file upload with progress with NodeJS

    http://jasonturim.wordpress.com/2013/09/12/angularjs-native-multi-file-upload-with-progress/

    ngUpload - An AngularJS Service for uploading files using iframe

    http://ngmodules.org/modules/ngUpload

    0 讨论(0)
  • 2020-11-22 09:45

    You can simply add the below code in onchange and it will detect change. you can write a function on X click or something to remove file data..

    document.getElementById(id).value = "";
    
    0 讨论(0)
提交回复
热议问题