简体   繁体   English

HTML和Javascript:使用输入文件获取完整目录路径名

[英]HTML and Javascript: getting full directory pathname using input file

From my research, I know that I cannot use HTML input file to get the directory pathname. 从我的研究中,我知道我不能使用HTML输入文件来获取目录路径名。 However, I tried an alternative method where I store the value of the input file into a hidden field and pass the stored value to my back end codes for further processing. 但是,我尝试了另一种方法,将输入文件的值存储到隐藏字段中,并将存储的值传递给后端代码以进行进一步处理。 Javascript code: Javascript代码:

<script type="text/javascript">
function folder_address()
{
    var address=document.getElementById('folder_address');
    var folder=document.getElementById('folder');
    folder.value=address.value;
}
</script>

HTML code: HTML代码:

<input type="file" id="folder_address" name="folder_address" />
<input type="hidden" id="folder" name="folder" />

However, I still get folder.value as the file that I selected and not the full directory pathname of the file. 但是,我仍然将folder.value作为我选择的文件,而不是文件的完整目录路径名。 Did I miss out anything? 我错过了什么吗?

This behavior isn't meant to be circumvented. 这种行为并不意味着被规避。 Even if you could find a loophole using script/additional fields, the loophole would likely be removed in the future. 即使您可以使用脚本/其他字段找到漏洞,也可能在将来删除漏洞。

I would strongly suggest a design that does not rely on knowing the local path which the user has selected. 我强烈建议一种不依赖于知道用户选择的本地路径的设计。

It would be a security flaw to reveal a user's directory structure to the server (think c:\\users\\tmedora in Windows; now you know my user name). 向服务器显示用户的目录结构是一个安全漏洞(想想Windows中的c:\\users\\tmedora ;现在你知道我的用户名)。 In addition, file systems can format paths differently and users are free to name/rename directories as they wish. 此外,文件系统可以以不同方式格式化路径,用户可以根据需要自由命名/重命名目录。 I'm not sure what value a full path would have to the server. 我不确定完整路径对服务器有什么价值。 Unless you are in an extremely controlled environment, you have no guarantee that you will even be able to parse the path, let alone count on it containing consistent information. 除非您处于极端受控的环境中,否则您无法保证甚至能够解析路径,更不用说依赖它包含一致的信息。

The W3 specifically states that including the full path is a security vulnerability and requires that user agents prepend the string c:\\fakepath\\ to file paths. W3 明确指出 ,包括完整路径是一个安全漏洞,并要求用户代理将字符串c:\\fakepath\\到文件路径。

Some legacy user agents actually included the full path (which was a security vulnerability). 一些旧版用户代理实际上包含完整路径(这是一个安全漏洞)。

Also see IE Gives full path, FF gives only filename (or directory browse) and How to get the full path of the file from a file input and many others. 另请参阅IE Gives完整路径,FF仅提供文件名(或目录浏览)以及如何从文件输入和许多其他文件中获取文件的完整路径

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM