-1

I am using var documentName = $('#documentFile').val().split('\\').pop(); my output is filename with extension.

The output which I wanted is only the file name without extension

Iam_NSA
  • 404
  • 1
  • 5
  • 12

4 Answers4

1

Once you have documentName as the filename without the path, you can get the filename without the extension like this:

let name = documentName;
const index = documentName.lastIndexOf(".");
if (index > 0) {
  name = documentName.substring(0,index);
}
// `name` now contains the name without the extension

Note that I'm using lastIndexOf to handle the case of multiple dots in a filename (like "file.name.txt"), and if there is no extension you just get the original name.

Always Learning
  • 5,510
  • 2
  • 17
  • 34
0

You'll want to use split again.

var documentName = $('#documentFile').val().split('\\').pop();
documentName.split('.').pop();
documentName.join('.');

Note that I've used a double backslash because the first one tries to escape the single quote, using double backslash will stop this from happening.

Mark
  • 1,852
  • 3
  • 18
  • 31
  • var without Extention will give only the extension...My expected output is only the file name, not the extension – Iam_NSA Aug 19 '19 at 08:46
0

You can use fileReader in js. In fileName you get only file name.

    if (window.File && window.FileReader && window.FileList && window.Blob) {
        const self = this;
        const file = event.target.files[0]
        let reader = new FileReader()
        reader.onload = function (event) {
           self.setState({ fileName: event.target.result });
        }

        reader.readAsText(file);

    }
Iiskaandar
  • 394
  • 2
  • 7
0

Firstly i believe that you have to escape your backslash so the correct way to extract your filename would be var documentName = $('#documentFile').val().split('\\').pop(). After this you can just chain a .split() to your code and it should work. you could try var documentName = $('#documentFile').val().split('\\').pop().split(".")[0]

Apoorv
  • 622
  • 8
  • 22