简体   繁体   English

无法使用Angular.js在文件名中添加随机数

[英]Can not add random number with file name using Angular.js

I need one help.I need to add one random number with the file name using Angular.js.I am explaining my code below. 我需要一个帮助,我需要使用Angular.js在文件名中添加一个随机数,我在下面解释我的代码。

<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( ie-curnum ) with the file name.Please help me. 在控制台消息中,我无​​法获得包含一些随机数的新文件名。这里我需要在文件名中包含一个随机数( ie-curnum )。请帮助我。

here my working jsfiddle 这是我的工作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 这是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>

You can use Upload.rename(file, newName) . 您可以使用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 . 为了在ui上显示文件的新名称,您可以使用将新名称保留在其中的作用域变量,也可以使用file.ngfName对其进行file.ngfName

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM