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