如何解决C:\ fakepath?

e_m*_*axm 231 html javascript dom file-upload

<input type="file" id="file-id" name="file_name" onchange="theimage();">
Run Code Online (Sandbox Code Playgroud)

这是我的上传按钮.

<input type="text" name="file_path" id="file-path">
Run Code Online (Sandbox Code Playgroud)

这是我必须显示文件完整路径的文本字段.

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

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

C:\fakepath\test.csv 
Run Code Online (Sandbox Code Playgroud)

和Mozilla给了我:

test.csv
Run Code Online (Sandbox Code Playgroud)

但我想要本地完全限定的文件路径.如何解决这个问题?

如果这是由于浏览器安全问题,那么应该采用哪种替代方法呢?

Joe*_*nos 161

某些浏览器具有安全功能,可防止JavaScript知道文件的本地完整路径.这是有道理的 - 作为客户端,您不希望服务器知道本地计算机的文件系统.如果所有浏览器都这样做会很好.

  • 只需发布表单:浏览器将负责上传.您的网站无需知道客户端上的完整路径. (42认同)
  • 如果浏览器没有发送本地文件路径,则无法找到它. (9认同)
  • 隐私几乎总是与安全性和漏洞相关,因为信息可以用来对付某人.攻击经常利用多个问题来实现目标.如果没有额外的偏执型工作坊,任何不承认危险的人都不应该被允许在Hello World以外的代码附近. (7认同)
  • 我怀疑普通网站在没有某种可信插件(如Flash或Java)的情况下可以对您的本地计算机做任何恶意.这更像是隐私的事情.例如,如果您从桌面上传,则需要让服务器知道您本地计算机或域上的用户名(`c:\ users\myname\desktop`或`/ Users/myname/Desktop /`或随你).这不是网络服务器有任何业务知识的东西. (6认同)
  • 没有,抱歉 (3认同)
  • @ruffp您只能上传帖子,因此您需要将上传内容放入表单并提交.仍然会使用标准文件上传控件来选择文件,但您仍然不需要完整的客户端文件名. (2认同)

小智 62

使用

document.getElementById("file-id").files[0].name; 
Run Code Online (Sandbox Code Playgroud)

代替

document.getElementById('file-id').value
Run Code Online (Sandbox Code Playgroud)

  • 在Chrome 44.0.2403.125中,这仅为您提供文件名. (15认同)
  • 只提供文件名...现在不正确答案 (4认同)
  • 然后使用document.getElementById("file-id").files [0] .path.对我来说很好. (4认同)
  • `document.getElementById("file-id").files[0].path` 不再工作@Loaderon? (4认同)

小智 36

如果您转到Internet Explorer,工具,Internet选项,安全性,自定义,找到"将文件上载到服务器时包括本地目录路径"(这是相当不错的方法)并单击"启用".这会奏效

  • 这是一个解决方案(充其量)不是解决方案 (4认同)
  • 有没有类似Chrome的东西? (3认同)

cha*_*ser 35

我在输入onchange事件中使用对象FileReader 作为输入文件类型!此示例使用readAsDataURL函数,因此您应该有一个标记.FileReader对象也有readAsBinaryString来获取二进制数据,以后可以用来在服务器上创建相同的文件

例:

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

  • 问题是要求文件**路径**而不是文件内容. (8认同)

小智 11

我很高兴浏览器能够将我们从侵入式脚本等中拯救出来.我不满意IE在浏览器中放入一些简单的样式修复看起来像黑客攻击!

我使用<span>来表示文件输入,以便我可以将适当的样式应用于<div>而不是<input>(再次,因为IE).现在由于这个IE想要向用户展示一个价值的路径,这个价值只是保证让他们保持警惕并且至少让人担心(如果不是完全吓唬他们的话!)...更多IE-CRAP!

无论如何,感谢那些在这里发布解释的人:IE浏览器安全:在输入[type ="file"]中将"伪路径"附加到文件路径,我把一个小修复器 - 上层...

下面的代码做了两件事 - 它修复了一个lte IE8错误,其中onChange事件在上传字段的onBlur之前不会触发,并且它更新了一个元素,其中清理的文件路径不会吓到用户.

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


I a*_*m L 9

在基于 Chrome/Chromium 的应用程序(例如 Electron)上,您可以只使用 target.files:

(我在这个例子中使用React JS)

const onChange = (event) => {
  const value = event.target.value;

  // this will return C:\fakepath\somefile.ext
  console.log(value);

  const files = event.target.files;

  //this will return an ARRAY of File object
  console.log(files);
}

return (
 <input type="file" onChange={onChange} />
)
Run Code Online (Sandbox Code Playgroud)

File object上面所说的看起来像这样:

{
  fullName: "C:\Users\myname\Downloads\somefile.ext"
  lastModified: 1593086858659
  lastModifiedDate: (the date)
  name: "somefile.ext"
  size: 10235546
  type: ""
  webkitRelativePath: ""
}
Run Code Online (Sandbox Code Playgroud)

所以fullName如果你想得到路径,你就可以得到。

请注意,这仅适用于 chrome/chromium 浏览器,因此如果您不必支持其他浏览器(例如您正在构建电子项目),则可以使用它。

  • 如果我在 Firefox 中的 https://jscomplete.com/playground 上运行你的示例(使用 `ReactDOM.render(&lt;input type="file" onChange={onChange} /&gt;, mountNode);`),我会得到一个对象,其属性包括“lastModified”、“name”、“size”、“type”和“webkitRelativePath”。事实上,正如其他人所说,能够获得完整的路径通常是一个安全/隐私问题。所以你一定做了其他事情才能达到上面的结果。 (5认同)

小智 6

我遇到了同样的问题.在IE8中,它可以通过在文件输入控件之后创建隐藏输入来解决.填充它的前一个兄弟的价值.在IE9中,这已得到修复.

我想要了解完整路径的原因是在上传之前创建一个javascript图像预览.现在我必须上传文件以创建所选图像的预览.