1

I need one help.I need to add one random number with the file name using Angular.js.I am explaining my code below.

<div ng-class="{'myError': billdata.regdoc.$touched && billdata.regdoc.$invalid }">
<input type="file" class="filestyle form-control" data-size="lg" name="regdoc" id="regdoc"  ng-model="regfile" ngf-pattern="application/pdf,application/vnd.ms-powerpoint,application/vnd.openxmlformats-officedocument.wordprocessingml.document" accept="application/pdf,application/vnd.ms-powerpoint,application/vnd.openxmlformats-officedocument.wordprocessingml.document"  ngf-select="onRegFileSelect($file);" >
</div>
</div>
<div class="help-block" ng-messages="billdata.regdoc.$error" ng-if="billdata.regdoc.$touched">
<p ng-message="pattern" style="color:#F00;">This field only accepts .pdf,.ppt,.docx files.</p>
</div>

My controller file code is given below.

var regDocURL='';
$scope.onRegFileSelect=function(files){
        console.log('docs details',files);
        regDocURL=files;
}
var curnum=(Math.random() * new Date().getTime()).toString(36).replace(/\./g, '');
var regDocs=regDocURL;
var newRegPath=curnum+"_"+ regDocs.name;
regDocs.name=newRegPath;
console.log('reg file',newRegPath,regDocs);

Here in the console message i can not get the new file name which is including some random number.Here i need to include one random number(i.e-curnum) with the file name.Please help me.

12
  • what are you using at the backend? Commented Dec 28, 2015 at 11:02
  • what do you get in the console message? Commented Dec 28, 2015 at 11:02
  • @RanganathanSwamy : I am using PHP for backend. Commented Dec 28, 2015 at 11:03
  • and anyway regDocs has your complete file and ng-file-upload module has nothing per say to change names because it used to pass on the file details to the backend. Commented Dec 28, 2015 at 11:04
  • You must do that at the backend. I have used ng-file-upload module and at the backend I use nodejs. Try regdocs.name, it is your best option otherwise you'll have to change it at the backend. Commented Dec 28, 2015 at 11:04

2 Answers 2

0

here my working jsfiddle

this is the controller

angular.module('app', ["ngFileUpload"]).controller('ctrl', function($scope) {

  $scope.onRegFileSelect=function(files){
      console.log('docs details',files);
      var curnum=(Math.random() * new Date().getTime()).toString(36).replace(/\./g, '');
      $scope.newname=curnum+"_"+ files.name;
  }
 });

this is HTML

<div data-ng-app="app" data-ng-controller="ctrl">
<input type="file" data-size="lg" ng-model="regfile" ngf-select ngf-change="onRegFileSelect($file);" >
<p>
{{newname}}
</p>
</div>
Sign up to request clarification or add additional context in comments.

7 Comments

@ Mauro Sala: what you did.Here i need to replace the new file name(i.e-newname) with existing file name(files).
Now what is that supposed to mean?
@RanganathanSwamy : suppose $scope.newname=1234_abc.pdf and before files.name=abc.pdf.After adding this random number the files.name should be 1234_abc.pdf.which is not happening.Please check my post again.
@subhra Please check the fiddle made by Mauro Sala, I believe his solution is what you need and if not I think you should explain your query more clearly.
@RanganathanSwamy :No,That was not my problem.any way i solved this using Upload.rename(regDocs, newRegPath); and it gave me the solution.
|
0

You can use Upload.rename(file, newName). This will upload the file with the new name.

For the purpose of showing the file's new name on the ui you can use the scope variable that you keep the new name in it or you can access it with file.ngfName.

Comments

Your Answer

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