Wie entferne ich fakepath im Webkit-Browser wie Chrome, Safari, Oper?
in IE und Firefox wird nur der Dateiname angezeigt, OK
Aber in Chrome, Oper, Safari. Es zeigt C:\fakepath\700.jpg
Wie kann ich C:\fakepath\in Chrome, Opera, Safari entfernen?.
<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>
Verwenden Sie einfach einen regulären Ausdruck, um alles vor (und einschließlich) dem letzten \
zu entfernen.
var path = "C:\\fakepath\\example.doc";
var filename = path.replace(/^.*\\/, "");
console.log(filename);
Offensichtlich erhalten Sie path
von Ihrer Dateieingabe.
Und Sie erhalten den Namen der ersten Datei.
document.getElementById("yourInputElement").files[0].name
Wenn Sie mehrere Dateinamen erhalten möchten, müssen Sie files
durchlaufen.
Etwas wie das sollte für Sie funktionieren:
<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>