Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

$(function () {
    $('#<%=File1.ClientID %>').change(function () {
         var uploadcontrol = document.getElementById('<%=File1.ClientID%>').value;
    })
})
like image 413
nksingh420 Avatar asked Dec 02 '13 08:12

nksingh420


2 Answers

Try this:

$('#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/

like image 103
Andrew Avatar answered Nov 14 '22 23:11

Andrew


You can split it with respect to last instance of dot('.')

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

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

But,

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

Take care of these things in a generic solution

  • get rid of / or go to the last /
  • if string has multiple dot('.') then only remove the string after last dot.
like image 22
AurA Avatar answered Nov 14 '22 23:11

AurA