This is the uploaded form.
<form class="alert alert-info">
<div>
<b id = "select_file" class="span3" style="font-weight: bold; cursor: pointer; ">Please select image</b>
<input class="span3" type="file" name="image_file" id="image_file" style="display:none " />
<input disabled="true" type="button" value="Upload image" class="btn" />
</div>
</form>
I use the following script to open a window with files. I want to show a file name in <b id = 'select_file'>
.
How can I do this?
$('#select_file').click(function(){
var _this = $(this);
$('#image_file').show().focus().click().hide();
var filename = $('#image_file').val();
_this.html(filename);
$('.btn').attr('disabled', false);
});
Try this to Get filename from input [type='file'] using jQuery.
Taken from @ jQueryPot
This was a very important issue for me in order for my site to be multilingual. So here is my conclusion tested in Firefox and Chrome.
jQuery trigger comes in handy.
So this hides the standard boring
type=file
labels. You can place any label you want and format anyway. I customized a script from http://demo.smarttutorials.net/ajax1/. The script allows multiple file uploads with thumbnail preview and uses PHP and MySQL.There is no jQuery function for this. You have to access the DOM element and check the files property.
Or
Getting the file name is fairly easy. As matsko points out, you cannot get the full file path on the user's computer for security reasons.
var file = $('#YOURID > input[type="file"]'); file.value; // filename will be,
In Chrome, it will be something like
C:\fakepath\FILE_NAME
orundefined
if no file was selected.It is a limitation or intention that the browser does not reveal the file structure of the local machine.
This isn't possible due to security reasons. At least not on modern browsers. This is because any code getting access to the path of the file can be considered dangerous and a security risk. Either you'll end up with an undefined value, an empty string or an error will be thrown.
When a file form is submitted, the browser buffers the file temporarily into an upload directory and only the temporary file name of that file and basename of that file is submitted.