How to get file name without extension in file upload using java script

I am going to upload a file using file upload control in asp.net. now i want to get the file name without extension using java script. I want to check the file name should only be an integer format

$(function () {
    $('#<%=File1.ClientID %>').change(function () {
         var uploadcontrol = document.getElementById('<%=File1.ClientID%>').value;
    })
})
+4
source share
2 answers

Try the following:

$('#test').change(function() {

      //something like C:\fakepath\1.html 
    var fpath = this.value;

    fpath = fpath.replace(/\\/g, '/');

    var fname = fpath.substring(fpath.lastIndexOf('/')+1, fpath.lastIndexOf('.'));

    console.log(fname);

});

http://jsfiddle.net/rooseve/Sdq24/

+6
source

You can break it in relation to the last instance of the point ('.')

var uploadcontrol = document.getElementById('<%=File1.ClientID%>').value;

uploadcontrol = uploadcontrol.split('.')[uploadcontrol.split('.').length - 2];

But,

this is true only for a simple case ... a much better general answer is given in How to get the file name from the full path using JavaScript?

  • / /
  • ('.'), .
+3

All Articles