0

please somebody help me out on this. I want to get the file name from <input type="file" id="photo1" name="photo1"/> I need the file name with no path and extension! I try to get the data with this var pic = document.getElementById("photo1").value; but is not working! Thank you in advance!

styven
  • 51
  • 1
  • 9

3 Answers3

0

I've created a fiddle for You: http://jsfiddle.net/rzvcy/1/

$(function(){
    $('#ddd').on('click' , function(e){
        e.preventDefault();
        var pic = document.getElementById("photo1").value;
        var answer = pic.split(/(\\|\/)/g).pop();
        alert(answer);
    });
});

Edit: In http://jsfiddle.net/rzvcy/3/ You've got only name of the file

    var answer = answer.split(/\./g);
Mr.TK
  • 1,743
  • 2
  • 17
  • 22
0

Try this: http://jsfiddle.net/BLGg4/1/

var btn = document.getElementById("clickme");

btn.onclick = function() {
   var pic = document.getElementById("photo1").value.split(/(\\|\/)/g).pop();   
   var filename = pic.substring(0, pic.lastIndexOf("."));
   alert(filename); 
};
Mat Richardson
  • 3,576
  • 4
  • 31
  • 56
0

I have created small fiddle for you please check a http://jsfiddle.net/7cVE5/

$("input[type=file]").change(function() {

     var filename = $(this).val().replace(/^.*[\\\/]/, '');
     $("#test_text_box").val(filename);

});

Sandeeproop
  • 1,756
  • 1
  • 12
  • 18
  • /^C:\\fakepath\\/ Really? What if he's gonna change a path or OS of server? He will have to change it in every .js file with this code. Sorry, but that's just not "cool" solution. :) – Mr.TK Apr 15 '14 at 12:15
  • This does: "replace(/^C:\\fakepath\\/i, '');" That was the code i did comment. Not current code. – Mr.TK Apr 16 '14 at 06:21
  • Ok :) Good we understand each other now :) – Mr.TK Apr 16 '14 at 07:19