如何在webkit浏览器中删除c伪路径,如chrome,safari,opera?

use*_*821 17 html javascript css safari google-chrome

如何删除webkit浏览器中的c伪路径,如chrome,safari,opera?

在IE和Firefox中它只显示文件名,没关系

但在Chrome,歌剧,野生动物园.它显示C:\ fakepath\700.jpg

如何在Chrome,opera,safari中删除C:\ fakepath \.

<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>
Run Code Online (Sandbox Code Playgroud)

Que*_*tin 24

只需使用正则表达式删除最后一个(包括)之前的所有内容\.

 var path = "C:\\fakepath\\example.doc";
 var filename = path.replace(/^.*\\/, "");
 console.log(filename);
Run Code Online (Sandbox Code Playgroud)

显然,你可以path从文件输入中获得.

  • 警告:这个解决方案很好,但并不完美.如果文件名包含反斜杠(例如,在Unix系统上),您将无法获得正确的文件名!更好的解决方案是使用`path.replace(/ ^ C:\\ fakepath \\ /,"")` (6认同)

Fus*_*ion 17

你会得到第一个文件的名称.

document.getElementById("yourInputElement").files[0].name
Run Code Online (Sandbox Code Playgroud)

如果你想获得多个文件名,你必须迭代files.