如何解决C:\ fakepath?

<input type="file" id="file-id" name="file_name" onchange="theimage();"> 

这是我的上传button。

 <input type="text" name="file_path" id="file-path"> 

这是我必须显示文件的完整path的文本字段。

 function theimage(){ var filename = document.getElementById('file-id').value; document.getElementById('file-path').value = filename; alert(filename); } 

这是解决我的问题的JavaScript。 但在警报值给我

 C:\fakepath\test.csv 

Mozilla给我:

 test.csv 

但我想要本地完全合格的文件path 。 如何解决这个问题?

如果这是由于浏览器的安全问题,那么应该怎么做呢?

某些浏览器具有防止JavaScript知道文件本地完整path的安全function。 这是有道理的 – 作为一个客户端,你不希望服务器知道你的本地机器的文件系统。 如果所有浏览器都这样做,那将会很好。

如果你去Internet Explorer,工具,Internet选项,安全,自定义,find“包括本地目录path上传文件到服务器”(这是相当一种方式),并点击“启用”。 这将工作

使用

 document.getElementById("file-id").files[0].name; 

代替

 document.getElementById('file-id').value 

我使用input文件types的inputonchange事件对象FileReader! 这个例子使用readAsDataURL函数,因此你应该有一个标签。 FileReader对象还具有readAsBinaryString以获取二进制数据,这些数据可以稍后用于在服务器上创build相同的文件

例:

 var input = document.getElementById("inputFile"); var fReader = new FileReader(); fReader.readAsDataURL(input.files[0]); fReader.onloadend = function(event){ var img = document.getElementById("yourImgTag"); img.src = event.target.result; } 

我很高兴浏览器能够帮助我们避免受到侵入性脚本等的影响。 我不喜欢IE浏览器,使一个简单的风格,修复看起来像一个黑客攻击的东西!

我使用<span>来表示文件input,以便我可以将适当的样式应用于<div>而不是<input>(再次,因为IE)。 现在,由于这个IE浏览器希望向用户显示一个值的path,只是保证了他们的警惕,并至less担心(如果不是完全吓跑他们?!)…更多IE-CRAP!

无论如何,感谢那些在这里发布的解释: IE浏览器安全:追加“fakepath”文件path中的input[types=“文件”] ,我已经把一个小修补程序上…

下面的代码做了两件事 – 它修复了IE8的错误,其中的onChange事件不会触发,直到上传字段的onBlur,它更新了一个清理的文件path,不会吓倒用户的元素。

 // self-calling lambda to for jQuery shorthand "$" namespace (function($){ // document onReady wrapper $().ready(function(){ // check for the nefarious IE if($.browser.msie) { // capture the file input fields var fileInput = $('input[type="file"]'); // add presentational <span> tags "underneath" all file input fields for styling fileInput.after( $(document.createElement('span')).addClass('file-underlay') ); // bind onClick to get the file-path and update the style <div> fileInput.click(function(){ // need to capture $(this) because setTimeout() is on the // Window keyword 'this' changes context in it var fileContext = $(this); // capture the timer as well as set setTimeout() // we use setTimeout() because IE pauses timers when a file dialog opens // in this manner we give ourselves a "pseudo-onChange" handler var ieBugTimeout = setTimeout(function(){ // set vars var filePath = fileContext.val(), fileUnderlay = fileContext.siblings('.file-underlay'); // check for IE's lovely security speil if(filePath.match(/fakepath/)) { // update the file-path text using case-insensitive regex filePath = filePath.replace(/C:\\fakepath\\/i, ''); } // update the text in the file-underlay <span> fileUnderlay.text(filePath); // clear the timer var clearTimeout(ieBugTimeout); }, 10); }); } }); })(jQuery); 

我遇到了同样的问题。 在IE8中,可以通过在文件input控制之后创build一个隐藏的input来解决这个问题。 用它的前一个兄弟值来填充它。 在IE9中这已经被修复了。

我希望了解完整path的原因是在上传之前创build一个JavaScript图像预览。 现在我必须上传文件来创build所选图像的预览。

如果你真的需要发送完整文件的path,那么你可能不得不使用类似于已签名的java applet,因为如果浏览器不发送这些信息,没有办法获取这些信息。

似乎你不能在js中findlocalhost的完整path,但你可以隐藏fakepath来显示文件名。 使用jQuery来获取没有path的文件input的选定文件名