1

I am using angular js fileModel directive to upload file, which is as below

angular.module('MyProject').directive('fileModel', ['$parse', function ($parse) {
    return {
        restrict: 'A',
        link: function(scope, element, attrs) {
            var model = $parse(attrs.fileModel);
            var modelSetter = model.assign;
            element.bind('change', function(){
                scope.$apply(function(){
                    modelSetter(scope, element[0].files[0]);
                });
            });
        }
    };
}]);

And I am using the same in html as below,

 <input type='file' class="imgInp" file-model="tenantLogoFile"/>

If I select one file then Its fine for me to capture the file chosen by user by using $watch on "tenantLogoFile". But if I am supposed to upload same file (Without refreshing page) immediately again, $watch does not get fired and ultimately I am unable upload the file.

I tried setting $scope.tenantLogoFile = null after first time successful upload, but no use, Can anyone help me please?

4
  • jsfiddle.net/ZG9re/7081 try this Commented Jul 14, 2017 at 10:14
  • Hi Aayushi I studied you code, But I did not understand, where is the call to "$scope.uploadFile" function? Commented Jul 14, 2017 at 10:25
  • Sorry forgot to update that, check this one: jsfiddle.net/ZG9re/7161 Commented Jul 14, 2017 at 10:29
  • Hi, This not working Aayushi.. Is this chrome problem??? I am fed up with this. Can you upload SAME FILE back to back without refreshing page? Commented Jul 14, 2017 at 10:35

2 Answers 2

6

Anyways guys I found a workaround as below,

<input type='file' file-model="myFile" ng-click="clearFileSelection();"/>

I wrote a function named "clearFileSelection" in controller and called it on every subsequent click on the file chooser button, so that the previous file contents got cleared, function code is as below.

$scope.clearFileSelection = function(){
        angular.element("input[type='file']").val(null);
    }

I hope this will definitely help some other strugglers like me.. Happyyy Coding..!!

Sign up to request clarification or add additional context in comments.

Comments

0

You don't have to alter your HTML or controller. As your directive is restrict to attribute. Its better use element.on('click',fn) for this scenario.

angular.module('MyProject').directive('fileModel', ['$parse', function ($parse) {
    return {
        restrict: 'A',
        link: function(scope, element, attrs) {
            var model = $parse(attrs.fileModel);
            var modelSetter = model.assign;
            *element.on('click', function(){
                angular.element("input[type='file']").val(null);
            });*
            element.bind('change', function(){
                scope.$apply(function(){
                    modelSetter(scope, element[0].files[0]);
                });
            });
        }
    };
}]);

1 Comment

Hi Krishan. I suggest you put your code into a markdown code fence. Meaning enclose it in 3 backticks like ``` lines of code go in here ``` . That will make your answer much easier to read.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.