使用 jQuery 的文件输入的完整路径
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3489133/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Full path from file input using jQuery
提问by gruszczy
When I call val()
on an input with type="file"
I only get file name rather than full path. How can I get full path?
当我调用val()
输入时,type="file"
我只得到文件名而不是完整路径。我怎样才能获得完整路径?
回答by Pekka
You can't: It's a security feature in all modern browsers.
你不能:这是所有现代浏览器的安全功能。
For IE8, it's off by default, but can be reactivated using a security setting:
对于IE8,它默认关闭,但可以使用安全设置重新激活:
When a file is selected by using the input type=file object, the value of the value property depends on the value of the "Include local directory path when uploading files to a server" security setting for the security zone used to display the Web page containing the input object.
The fully qualified filename of the selected file is returned only when this setting is enabled. When the setting is disabled, Internet Explorer 8 replaces the local drive and directory path with the string C:\fakepath\ in order to prevent inappropriate information disclosure.
当使用 input type=file 对象选择文件时,value 属性的值取决于用于显示网页的安全区域的“将文件上传到服务器时包括本地目录路径”安全设置的值包含输入对象。
仅当启用此设置时,才返回所选文件的完全限定文件名。当该设置被禁用时,Internet Explorer 8 将本地驱动器和目录路径替换为字符串 C:\fakepath\ 以防止不适当的信息泄露。
In all other current mainstream browsers I know of, it is also turned off. The file name is the best you can get.
在我所知道的所有其他当前主流浏览器中,它也被关闭。文件名是你能得到的最好的。
More detailed info and good links in this question. It refers to getting the value server-side, but the issue is the same in JavaScript before the form's submission.
此问题中的更多详细信息和良好链接。它指的是在服务器端获取值,但在表单提交之前,JavaScript 中的问题是相同的。
回答by DWX
Well, getting full path is not possible but we can have a temporary path.
好吧,获得完整路径是不可能的,但我们可以有一个临时路径。
Try This:
尝试这个:
It'll give you a temporary path not the accurate path, you can use this script if you want to show selected images as in this jsfiddle example(Try it by selectng images as well as other files):-
它会给你一个临时路径而不是准确的路径,如果你想在这个 jsfiddle 示例中显示选定的图像,你可以使用这个脚本(通过选择图像和其他文件来尝试):-
Here is the code :-
这是代码:-
HTML:-
HTML:-
<input type="file" id="i_file" value="">
<input type="button" id="i_submit" value="Submit">
<br>
<img src="" width="200" style="display:none;" />
<br>
<div id="disp_tmp_path"></div>
JS:-
JS:-
$('#i_file').change( function(event) {
var tmppath = URL.createObjectURL(event.target.files[0]);
$("img").fadeIn("fast").attr('src',URL.createObjectURL(event.target.files[0]));
$("#disp_tmp_path").html("Temporary Path(Copy it and try pasting it in browser address bar) --> <strong>["+tmppath+"]</strong>");
});
Its not exactly what you were looking for, but may be it can help you somewhere.
它不完全是您要找的东西,但可能可以在某处帮助您。