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?
当我在type =“file”的输入上调用val()时,我只获取文件名而不是完整路径。我怎样才能获得完整的路径?
3 个解决方案
#1
45
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.
使用input type = file对象选择文件时,value属性的值取决于用于显示网页的安全区域的“将文件上载到服务器时包括本地目录路径”的安全设置值包含输入对象。
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.
仅当启用此设置时,才会返回所选文件的完全限定文件名。禁用该设置后,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中的问题是相同的。
#2
14
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例子中显示选定的图像(通过selectng图像以及其他文件尝试): -
的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.
它不完全是你想要的,但可能它可以帮助你在某个地方。
#3
-17
as indicated above this is usually permission issue. try and and move the file to a different area and grant it full permissions.
如上所述,这通常是许可问题。尝试并将文件移动到其他区域并授予其完全权限。
var path = $('#file').attr("value");
var path = $('#file')。attr(“value”);
#1
45
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.
使用input type = file对象选择文件时,value属性的值取决于用于显示网页的安全区域的“将文件上载到服务器时包括本地目录路径”的安全设置值包含输入对象。
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.
仅当启用此设置时,才会返回所选文件的完全限定文件名。禁用该设置后,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中的问题是相同的。
#2
14
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例子中显示选定的图像(通过selectng图像以及其他文件尝试): -
的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.
它不完全是你想要的,但可能它可以帮助你在某个地方。
#3
-17
as indicated above this is usually permission issue. try and and move the file to a different area and grant it full permissions.
如上所述,这通常是许可问题。尝试并将文件移动到其他区域并授予其完全权限。
var path = $('#file').attr("value");
var path = $('#file')。attr(“value”);