How to resolve the C:\fakepath?
1 | <input type="file" id="file-id" name="file_name" onchange="theimage();"> |
这是我的上传按钮。
1 | <input type="text" name="file_path" id="file-path"> |
这是文本字段,我必须在其中显示文件的完整路径。
1 2 3 4 5 | function theimage(){ var filename = document.getElementById('file-id').value; document.getElementById('file-path').value = filename; alert(filename); } |
这是解决我问题的JavaScript。 但是在警戒值给了我
1 | C:\\fakepath\\test.csv |
Mozilla给了我:
1 | test.csv |
但是我想要本地标准文件路径。 如何解决这个问题?
如果这是由于浏览器安全性问题造成的,那么应该采取什么替代方法呢?
某些浏览器具有一项安全功能,该功能阻止JavaScript知道文件的本地完整路径。这很有意义-作为客户端,您不希望服务器知道本地计算机的文件系统。如果所有浏览器都这样做,那将是很好的。
用
1 | document.getElementById("file-id").files[0].name; |
代替
1 | document.getElementById('file-id').value |
我在输入
例:
1 2 3 4 5 6 7 | 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; } |
如果您转到Internet Explorer,工具,Internet选项,安全性,自定义,请找到"将文件上传到服务器时包含本地目录路径"(相当麻烦),然后单击"启用"。这会工作
我很高兴浏览器关心将我们从侵入性脚本等中拯救出来。我不满意IE在浏览器中添加一些使简单的样式修复看起来像黑客攻击的东西!
我使用表示文件输入,以便可以对
无论如何,这要归功于那些在此处发布解释的人:IE浏览器安全性:在input [type =" file"]中将" fakepath"追加到文件路径中后,我整理了一个较小的fixer-upper。
下面的代码有两件事-修复了IE8的一个IE8错误,该错误在上载字段的onBlur之前不会触发onChange事件,并且它使用不会使用户感到恐惧的干净文件路径来更新元素。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 | // 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 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中,可以通过在文件输入控件之后创建隐藏输入来解决此问题。用它的前一个兄弟的值填充它。在IE9中,此问题也已修复。
我想了解完整路径的原因是在上传之前创建了javascript图像预览。现在,我必须上传文件以创建所选图像的预览。
如果您确实需要发送上层文件的完整路径,则可能必须使用签名的Java applet之类的东西,因为如果浏览器不发送该信息,则无法获取任何信息。
您为什么不只使用target.files?
(我在这个例子中使用React JS)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | 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} /> ) |
我在上面说的
1 2 3 4 5 6 7 8 9 | { fullName:"C:\\Users\\myname\\Downloads\\somefile.ext" lastModified: 1593086858659 lastModifiedDate: (the date) name:"somefile.ext" size: 10235546 type:"" webkitRelativePath:"" } |
因此,如果您想获取路径,则只需获取
使用文件阅读器:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | $(document).ready(function() { $("#input-file").change(function() { var length = this.files.length; if (!length) { return false; } useImage(this); }); }); // Creating the function function useImage(img) { var file = img.files[0]; var imagefile = file.type; var match = ["image/jpeg","image/png","image/jpg"]; if (!((imagefile == match[0]) || (imagefile == match[1]) || (imagefile == match[2]))) { alert("Invalid File Extension"); } else { var reader = new FileReader(); reader.onload = imageIsLoaded; reader.readAsDataURL(img.files[0]); } function imageIsLoaded(e) { $('div.withBckImage').css({ 'background-image':"url(" + e.target.result +")" }); } } |
似乎您无法通过js在本地主机中找到完整路径,但是您可以隐藏fakepath来仅显示文件名。使用jQuery获取文件输入,选择的文件名不带路径
您将至少能够获得计算机上文件路径的临时创建的副本。这里唯一的条件是您的输入元素应在表格内
您要做的就是将属性
1 | <form id="formid" enctype="multipart/form-data" method="post" action="{{url('/add_a_note' )}}">...</form> |
您可以在底部找到路径字符串。
它将流打开到文件,然后将其删除。
嗨,在我的情况下,我正在使用asp.net开发环境,因此我想将这些数据上传到异步ajax请求中,在[webMethod]中您无法捕获文件上传器,因为它不是静态元素,
因此,我必须通过固定路径来解决此类问题,而不是将所需的图像转换为字节以将其保存在DB中。
这是我的javascript函数,
希望它可以帮助您:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 | function FixPath(Path) { var HiddenPath = Path.toString(); alert(HiddenPath.indexOf("FakePath")); if (HiddenPath.indexOf("FakePath") > 1) { var UnwantedLength = HiddenPath.indexOf("FakePath") + 7; MainStringLength = HiddenPath.length - UnwantedLength; var thisArray =[]; var i = 0; var FinalString=""; while (i < MainStringLength) { thisArray[i] = HiddenPath[UnwantedLength + i + 1]; i++; } var j = 0; while (j < MainStringLength-1) { if (thisArray[j] !=",") { FinalString += thisArray[j]; } j++; } FinalString ="~" + FinalString; alert(FinalString); return FinalString; } else { return HiddenPath; } } |
这里仅用于测试:
1 2 3 4 | $(document).ready(function () { FixPath("hakounaMatata:/7ekmaTa3mahaLaziz/FakePath/EnsaLmadiLiYghiz"); }); // this will give you : ~/EnsaLmadiLiYghiz |
补充Sardesh Sharma的答案:
1 | document.getElementById("file-id").files[0].path |
完整路径。