1

is there any way to change the file name dynamically in angular js

<input type="file" onchange="angular.element(this).scope().filename(this)">

and in filename method i am changing the file name but value is not changing how i can i change the file name in file upload

$scope.filename= function(e)
{

 for (var i = 0; i < e.files.length; i++) {

 var x =  e.files[i].name + "test";
 e.files[i].name =  x;
 /// here i am changing the file name but it shows its 
  read only property how can i change the name
  }
}

I want to change the upload file name...I have to assign file name for every uploaded file how?

3 Answers 3

1

Try this

var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope) {
  $scope.filename = function(e) {
    for (var i = 0; i < e.files.length; i++) {

      var x = e.files[i].name.split('.')[0] + "test." + e.files[i].name.split('.')[1];
      console.log(x);
      e.files[i].name = x;

    }
  }

});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>

<div ng-app="myApp" ng-controller="myCtrl">

  <input type="file" onchange="angular.element(this).scope().filename(this)">

</div>

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

1 Comment

while your are assigning this x variable to this e.files[i].name = x; value is not assigning for "e.files[i].name" not changing to new value
0

Since you are using the angularJS, You can use a directive to change the file name.

.directive('fileName', function() {
  return {
    scope: {
      fileName: '='
    },
    link: function(scope, el, attrs) {
      el.bind('change', function(event) {
        var files = event.target.files;
        var file = files[0];
        scope.file = 'Newname';
        scope.$apply();
      });
    }
  };
});

Call the directive from the element.

<input type="file" file-name="fileName">{{fileName}}

Comments

0

Its readonly value, So you can use directive change file name. Try this code

var module = angular.module('myApp', []);
module.directive('file', function() {
  return {
    scope: {
      file: '='
    },
    link: function(scope, e, attrs) {
      e.bind('change', function(event) {
        var files = event.target.files;
        var file = files[0];
        scope.file = file.name.split('.')[0] + "_test." + file.name.split('.')[1];
        scope.$apply();
      });
    }
  };
});

function MainController($scope) {
  $scope.file = {};
}
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="myApp" ng-controller="MainController">
  <input type="file" data-file="file.file" />
  <div>Update File Name: {{file.file}}</div>


</div>

Comments

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.