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!
Asked
Active
Viewed 92 times
0

styven
- 51
- 1
- 9
-
does that get full path or don't get anything? – Navin Rauniyar Apr 15 '14 at 11:32
-
Solved in earlier post.. [Try this](http://stackoverflow.com/questions/857618/javascript-how-to-extract-filename-from-a-file-input-control) – thsorens Apr 15 '14 at 11:33
-
This might be helpful to you: http://stackoverflow.com/questions/857618/javascript-how-to-extract-filename-from-a-file-input-control – Navin Rauniyar Apr 15 '14 at 11:33
3 Answers
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
-
thank you very much it's helped me out a little, but I want only the name, not the extension too. – styven Apr 15 '14 at 11:49
-
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
-