【问题标题】:Get file path on google chrome在谷歌浏览器上获取文件路径
【发布时间】:2017-06-22 03:16:14
【问题描述】:

我在尝试获取 chrome 上的文件路径时遇到问题。这是我的带有 javascript 函数的代码:

<html>
<head>
<script language="javascript" type="text/javascript">
function getPath() {
 var inputName = document.getElementById('ctrl');
 var imgPath;

 imgPath = inputName.value;
 //alert(imgPath);
 var x = document.createElement("INPUT");
x.setAttribute("type", "text");
x.setAttribute("value", imgPath);
document.body.appendChild(x);
}
</script>
</head>
<body>


<input type="file" name ="file1" id="ctrl" webkitdirectory directory multiple/>
<input type="submit" value="Enviar" onclick="getPath()"/>

</body>
</html>

当我按下按钮时,文本字段会显示这个

C:\fakepath\ART.pdf

我尝试编辑 Internet Explorer 设置,它工作正常,但我无法在 chrome 上获得完整路径 有什么方法可以获取 chrome 上的完整路径吗?谢谢

【问题讨论】:

  • 为什么需要路径?
  • 允许用户选择要保存一些文件的目录
  • 出于明显的安全原因,您无法访问它。您希望任何网站都能看到您的目录结构吗?
  • 基本上是chrome,因为我的icefaces项目在ie中效果不佳。

标签: javascript html google-chrome


【解决方案1】:

您正在使用多个和目录选项,因此您的路径将在 inputName.files 下可用

出于安全原因,这些将是相对路径。如果您想读取文件,您需要使用 FileReader API。

https://developer.mozilla.org/en-US/docs/Web/API/FileReader

我在 cmets 中读到您正在尝试将文件保存到特定位置。这无法通过浏览器实现。您只需要提供要下载的文件,用户将决定保存它的位置。通常会保存到操作系统的默认下载文件夹中。

【讨论】:

  • 但是 fileReader API 并不能帮助我获得完整的路径。它还向我展示了 fakepath
猜你喜欢
  • 1970-01-01
  • 2011-03-13
  • 2011-04-26
  • 1970-01-01
  • 1970-01-01
  • 2020-12-31
  • 2017-09-23
  • 2021-11-28
  • 1970-01-01
相关资源
最近更新 更多