最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Cannot replace the file name using Angular.js ng-file-upload - Stack Overflow

programmeradmin1浏览0评论

I am trying to rename the file name after uploading the file using Angular.js ng-file-upload. But what is happening in my case when I am removing ngf-min-height="400" ngf-resize="{width: 400, height:400}" properties. I am providing my code below:

<input type="file"  data-size="lg" name="bannerimage" id="imagefile1"  ng-model="file" ngf-pattern="image/*" accept="image/*"  ngf-max-size="2MB" ngf-min-height="400" ngf-resize="{width: 400, height:400}"   custom-on-change="uploadFile"  ngf-select="onFileSelect($file);" >

My controller side code is given below:

$scope.onFileSelect = function($files) {
         fileURL=$files;
         $scope.imageData='';
    }
var file=fileURL;
var today=(Math.random() * new Date().getTime()).toString(36).replace(/\./g, '');
var newpath=today+"_"+ file.name;
file.name=newpath;

In the above code I am adding a random number to file name and replacing it. In this case it's ing properly, but if I am removing ngf-min-height="400" ngf-resize="{width: 400, height:400}" from file input like below.

<input type="file"  data-size="lg" name="bannerimage" id="imagefile1"  ng-model="file" ngf-pattern="image/*" accept="image/*"  ngf-max-size="2MB"   custom-on-change="uploadFile"  ngf-select="onFileSelect($file);" >

In this case I cannot replace the new file name and I don't need to restrict the file height and width.

I am trying to rename the file name after uploading the file using Angular.js ng-file-upload. But what is happening in my case when I am removing ngf-min-height="400" ngf-resize="{width: 400, height:400}" properties. I am providing my code below:

<input type="file"  data-size="lg" name="bannerimage" id="imagefile1"  ng-model="file" ngf-pattern="image/*" accept="image/*"  ngf-max-size="2MB" ngf-min-height="400" ngf-resize="{width: 400, height:400}"   custom-on-change="uploadFile"  ngf-select="onFileSelect($file);" >

My controller side code is given below:

$scope.onFileSelect = function($files) {
         fileURL=$files;
         $scope.imageData='';
    }
var file=fileURL;
var today=(Math.random() * new Date().getTime()).toString(36).replace(/\./g, '');
var newpath=today+"_"+ file.name;
file.name=newpath;

In the above code I am adding a random number to file name and replacing it. In this case it's ing properly, but if I am removing ngf-min-height="400" ngf-resize="{width: 400, height:400}" from file input like below.

<input type="file"  data-size="lg" name="bannerimage" id="imagefile1"  ng-model="file" ngf-pattern="image/*" accept="image/*"  ngf-max-size="2MB"   custom-on-change="uploadFile"  ngf-select="onFileSelect($file);" >

In this case I cannot replace the new file name and I don't need to restrict the file height and width.

Share Improve this question edited Feb 16, 2019 at 22:21 halfer 20.4k19 gold badges109 silver badges202 bronze badges asked Apr 20, 2016 at 4:08 satyasatya 3,56011 gold badges59 silver badges134 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 4

The Upload Service of ng-file-upload has a method for this. Here is an example:

$scope.$watch('dropFile', function () {
    uploadFile($scope.dropFile);
});

function uploadFile(file) {
  if (!file) {
    return;
  }

  file = Upload.rename(file, "newName.ext");

  Upload.upload({
        url: <api url for upload>,
        data: {file: file}
    }).then(function (resp) {
      //successful
    }, function (resp) {
        //error
    }, function (evt) {
       //progress
    });
}

try this code

<input type="file"  data-size="lg" name="bannerimage" id="imagefile1"  ng-model="file" accept="image/*" ng-file-select="onFileSelect($files[0]);" >

$scope.onFileSelect =function(file){
    filename = 'jihin.' + file.name.split(".")[1];
    var imgFile = new File([file], filename, {type:file.type});
    console.log(imgFile);
};
发布评论

评论列表(0)

  1. 暂无评论