繁体   English   中英

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

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

我需要一个帮助,我需要使用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>

我的控制器文件代码如下。

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);

在控制台消息中,我无​​法获得包含一些随机数的新文件名。这里我需要在文件名中包含一个随机数( ie-curnum )。请帮助我。

这是我的工作jsfiddle

这是控制器

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;
  }
 });

这是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>

您可以使用Upload.rename(file, newName) 这将以新名称上传文件。

为了在ui上显示文件的新名称,您可以使用将新名称保留在其中的作用域变量,也可以使用file.ngfName对其进行file.ngfName

暂无
暂无

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

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