1

I am having an issue with changing a file name.

The main issue I am having is when the user takes a picture from an iPhone. iOS names all just captured photos image.jpg. I am trying to give the user a chance to change the file name or do something programmatically, like "image (1).jpg".

I am looking for some advice on renaming files in JS or some other idea on how to handle preventing users from uploading a file with the same name.

function processSelectedFiles(fileInput) {
  var files = fileInput.files;
  var div = document.getElementById("fileList");

  for (var i = 0; i < files.length; i++) {
    var newFileName = prompt("Filename " + files[i].name,files[i].name);
    
    if (newFileName!== null){
      files[i].name=newFileName;
    }
    
    div.innerText += files[i].name;
  }
}
<!-- Learn about this code on MDN: https://developer.mozilla.org/en-US/docs/Web/API/File/name -->

<input type="file" multiple onchange="processSelectedFiles(this)">
</br>
File Name:
</br>
<div id="fileList"></div>

2
  • 1
    Why? Once it is uploaded to the server you can rename it anyways. Commented Mar 4, 2019 at 14:01
  • I want to give the user a chance to rename the file or accept my name change Commented Mar 4, 2019 at 15:29

1 Answer 1

2

For security reason javascript in browser doesn't have the access to the file system. So if you try to get the full path from front end it's not possible check here

https://www.w3schools.com/jsref/tryit.asp?filename=tryjsref_fileupload_value

You can create an api and hook it in the front end for making any file name changing operations and etc.

There are some alternative approaches provided by Firefox(mozFullPath) but it's not widely accepted at this point.

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.