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 coming 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.

2 Answers 2

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
    });
}
Sign up to request clarification or add additional context in comments.

1 Comment

nice! I was trying to find something like that. thank you
2

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

2 Comments

ok,i will try this but its ok with ngf-min-height="400" ngf-resize="{width: 400, height:400}" when i am removing this its not happening.Why?it need to be resolved.
use ng-file-select="onFileSelect($files[0]);" insted of ngf-select @satya

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.