How to remove c fakepath in webkit browser like chrome, safari, opera ?
in IE And Firefox it's show only file name , it's OK
But in Chrome, opera, safari. It's show C:\fakepath\700.jpg
How can i remove C:\fakepath\ in Chrome, opera, safari.
<script type="text/javascript" src=".6.4.js"></script>
<style type="text/css">
.inputWrapper {
overflow: hidden;
position: relative;
cursor: pointer;
/*Using a background color, but you can use a background image to represent a button*/
background-color: #DDF;
}
.fileInput {
cursor: pointer;
height: 100%;
position:absolute;
top: 0;
right: 0;
/*This makes the button huge so that it can be clicked on*/
font-size:50px;
}
.hidden {
/*Opacity settings for all browsers*/
opacity: 0;
-moz-opacity: 0;
filter:progid:DXImageTransform.Microsoft.Alpha(opacity=0)
}
</style>
<script type="text/javascript">//<![CDATA[
$(window).load(function(){
$(function() {
$(".inputWrapper").mousedown(function() {
var button = $(this);
button.addClass('clicked');
setTimeout(function(){
button.removeClass('clicked');
},50);
});
$('input[type=file]').change(function() {
var $this = $(this);
$this.parent().find('span').text($this.val());
});
});
});//]]>
</script>
<div class="inputWrapper" id="inputWrapper" style="height: 56px; width: 128px;">
<input class="fileInput hidden" type="file" name="file2"/>
<span></span>
</div>
How to remove c fakepath in webkit browser like chrome, safari, opera ?
in IE And Firefox it's show only file name , it's OK
But in Chrome, opera, safari. It's show C:\fakepath\700.jpg
How can i remove C:\fakepath\ in Chrome, opera, safari.
<script type="text/javascript" src="http://code.jquery.com/jquery-1.6.4.js"></script>
<style type="text/css">
.inputWrapper {
overflow: hidden;
position: relative;
cursor: pointer;
/*Using a background color, but you can use a background image to represent a button*/
background-color: #DDF;
}
.fileInput {
cursor: pointer;
height: 100%;
position:absolute;
top: 0;
right: 0;
/*This makes the button huge so that it can be clicked on*/
font-size:50px;
}
.hidden {
/*Opacity settings for all browsers*/
opacity: 0;
-moz-opacity: 0;
filter:progid:DXImageTransform.Microsoft.Alpha(opacity=0)
}
</style>
<script type="text/javascript">//<![CDATA[
$(window).load(function(){
$(function() {
$(".inputWrapper").mousedown(function() {
var button = $(this);
button.addClass('clicked');
setTimeout(function(){
button.removeClass('clicked');
},50);
});
$('input[type=file]').change(function() {
var $this = $(this);
$this.parent().find('span').text($this.val());
});
});
});//]]>
</script>
<div class="inputWrapper" id="inputWrapper" style="height: 56px; width: 128px;">
<input class="fileInput hidden" type="file" name="file2"/>
<span></span>
</div>
Share
Improve this question
asked Jan 29, 2014 at 8:54
user3215821user3215821
2632 gold badges4 silver badges11 bronze badges
2
- possible duplicate of Use jQuery to get the file input's selected filename without the path – Joel H. Commented Sep 17, 2015 at 14:21
- 1 Please review answers and mark one. – Fusion Commented May 30, 2016 at 11:22
4 Answers
Reset to default 28Just use a regular expression to remove everything before (and including) the last \
.
var path = "C:\\fakepath\\example.doc";
var filename = path.replace(/^.*\\/, "");
console.log(filename);
Obviously, you'd get path
from your file input.
And you will get the name of first file.
document.getElementById("yourInputElement").files[0].name
If you want to get multiple filenames, you have to iterate over files
.
Something like the following should work for you:
<script type="text/javascript">
$(function() {
$(".inputWrapper").mousedown(function() {
var button = $(this);
button.addClass('clicked');
setTimeout(function(){
button.removeClass('clicked');
},50);
});
$('input[type=file]').on('change', function(e) {
var filename = $(e.currentTarget).val().replace(/^.*\\/, "");
$this.parent().find('span').text(filename);
});
});
</script>
Use Below Code :
<label for="file" class="input input-file"><div class="button"><input type="file" onchange="this.parentNode.nextSibling.value = this.value" name="uploadfiliron" class="uploadimg" data-gotfile='uploadimg-1'>Browse</div><input type="text" readonly class="uploadimg-1"></label>
And This is script
<script>$('body').on('change','.uploadimg',function(e){
var filename = $(this).val().replace(/.*(\/|\\)/, '');
var getft = $(this).attr('data-gotfile');
console.log(getft+" "+filename);
if(filename=='')
{
$('.'+getft).val('No file chosen');
}
else
{
$('.'+getft).val('Your file name: '+filename);
}});</script>