2014-01-29 23 views
13

如何删除像chrome,safari,opera这样的webkit浏览器中的c fakepath?如何删除像chrome,safari,opera这样的webkit浏览器中的c fakepath?

在IE和Firefox

这是只显示文件名,这是确定

但在Chrome,歌剧,Safari浏览器。这是显示C:\ fakepath \ 700.jpg

我如何删除Chrome,歌剧,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> 
+0

的可能重复[使用jQuery来获取文件输入的不带路径选择的文件名(http://stackoverflow.com/questions/6365858/use-jquery-to-get-the-file-inputs-selected-filename-without-the-path) –

+1

请检查答案并将其标记出。 – Fusion

回答

19

只需使用正则表达式来删除之前的一切(包括)最后\

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

很明显,您会从文件输入中获得path

+0

请给我演示一下我的代码,谢谢^^ – user3215821

+0

演示如下。感谢@Quentin –

+5

警告:此解决方案很好,但不完美。如果文件名包含反斜线(例如,在Unix系统上),您将无法获得正确的文件名!更好的解决方案是使用'path.replace(/^C:\\ fakepath \\ /,“”)' – stackular

0

像下面这样的东西应该为你工作:

<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> 
-2

其实,当你有包含在你的路径的子文件夹也不起作用和JavaScript不能做任何事情来你的文件系统

所以这里是一个reg文件,将解决这个问题

< fakepath_fix.reg>

;下面的代码是用于企业内部网

[HKEY_CURRENT_USER \软件\微软\的Windows \ CurrentVersion \ Internet设置\区\ 1] “160A”= DWORD:00000000

;下面的代码是为受信任的站点

[HKEY_CURRENT_USER \软件\微软\的Windows \ CurrentVersion \ Internet设置\区\ 2] “160A”= DWORD:00000000

;下面的代码是互联网

[HKEY_CURRENT_USER \ SOFTWARE \微软\的Windows \ CurrentVersion \ Internet设置\区域\ 3] “160A”= DWORD:00000000

;下面的代码是用于限制区

[HKEY_CURRENT_USER \ SOFTWARE \微软\视窗\ CurrentVersion \ Internet Settings \ Zones \ 4] “160A”= dword:00000000

9

您将获得第一个文件的名称。

document.getElementById("yourInputElement").files[0].name 

如果你想获得多个文件名,你必须遍历files

+0

是的..此外,你甚至不需要jQuery。 – Fusion

-3

使用下面的代码:

<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> 

,这是脚本

<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> 
+0

Unix文件名可以包含反斜杠,这个解决方案没有考虑到这一点。 –

相关问题