0

Is there any way to change choosen file name? For example, I choose a file name img1. On choosing the file it have to change to dynamicname. Is there any way to change name?

<input type="file" fd-input/>

enter image description here

3
  • 3
    Provide your code, instead of a screenshot, please. Commented Feb 21, 2017 at 10:21
  • Hi Mistalis , Its normal <input type="file">, i need to change selected image name to dynamic name. Commented Feb 21, 2017 at 10:27
  • Are you asking if you can rename a file on the user's computer? Commented Feb 27, 2017 at 7:48

2 Answers 2

1

Here is a way to change a filename inside a directive:

app.directive('file', function() {
  return {
    scope: {
      file: '='
    },
    link: function(scope, el, attrs) {
      el.bind('change', function(event) {
        var files = event.target.files;
        var file = files[0];
        scope.file = 'New file name';
        scope.$apply();
      });
    }
  };
});

Use it as following:

<input type="file" file="param.file" />

JSFiddle demo

Sign up to request clarification or add additional context in comments.

Comments

0

If you have access to FormData you can use append method to change file name.

Refer this doc for more information.

var files = $("#fileUpload").get(0).files;
    if (files.length > 0) {
        var data = new FormData();
        var ext = files[0].name.match(/\.(.+)$/)[1];
        var fileName = Math.random().toString(36).substr(2, 19) + "." + ext;
        // Add the uploaded image content to the form data collection
        if (files.length > 0) {
            //data.append("UploadedImage", files[0]); //if you want to pass default name
            data.append("UploadedImage", files[0],fileName);
        } 

HTML code -

<input id="fileUpload" type="file" ng-file-accept="'.jpg , .jpeg ,.bmp , .gif ,.png'" accept="image/*" />

Comments

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.