0

This is my HTML form

<!DOCTYPE html>
<html>
<body>

<form>
  <input type="file" name="pic">
  <input type="submit">
</form>


</body>
</html>

It displays a simple file picker.

As soon as the "choosing the file" is completed I would like to rename the name of the file selected.

Is it possible to do this in jQuery ?

For example, this is my UI initially

enter image description here

I choose a file "Screenshot_1.png"

then it looks like

enter image description here

As soon as this action is done I want the file name to be renamed as "XYZ_Screenshot_1.png"

Is it possible to do this in jQuery ?

1

1 Answer 1

2

give id or class to your file select control

<input type="file" class="filepath" id="filepath" />

then call the function on click of submit button in javascript

function submitclick(){
 var fileUpload = $(".filepath").get(0);
            var files = fileUpload.files;
            var test = new FormData();
            for (var i = 0; i < files.length; i++) {
                var currentdate = new Date();
                var d = currentdate.getDate();
                var m = currentdate.getMonth() + 1; // JavaScript months are 0-11
                var y = currentdate.getFullYear();
                var h = currentdate.getHours();
                var mn = currentdate.getMinutes();
                var sc = currentdate.getSeconds();
                var fname = "filename" + "_" + d + "_" + m + "_" + y + "_" + h + "_" + mn + "_" + sc;
                var parts = files[i].name.split('.');

                test.append(fname + "." + parts[parts.length - 1], files[i]);
                FileUploaded = fname + "." + parts[parts.length - 1];
}

then you can pass this file to upload method

  $.ajax({
                url: "uploadurl",
                type: "POST",
                contentType: false,
                processData: false,
                data: test,
                // dataType: "json",
                success: function (result) {}
});
Sign up to request clarification or add additional context in comments.

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.