【发布时间】:2016-05-07 18:40:09
【问题描述】:
我有一个基于 Web 的应用程序,我需要能够从中弹出一个文件打开对话框并让用户选择一个文件夹,然后将其路径填充到 HTML 表单字段中,以便用户不不必输入。
没有上传文件或文件夹,只有路径信息。
客户端平台/环境是带有 Internet Explorer 11 的 Windows 7,或者 OS X 上的可选 Safari(最新版本)。它不能使用 Java、Flash 或任何其他必须下载和安装的插件 - 只能使用已安装的插件默认情况下在这些平台上,例如 Windows 上的 ActiveX。
我发现这段代码几乎能满足我的需要——但我不知道如何让它选择一个文件夹而不仅仅是一个非文件夹文件:
<html>
<head>
<script type="text/javascript" language="JavaScript">
if (typeof String.prototype.startsWith != 'function') {
// see below for better implementation!
String.prototype.startsWith = function (str){
return this.indexOf(str) == 0;
};
}
function getUNCPath() {
var fileName = document.getElementById("uploadedFile").value;
var WshNetwork = new ActiveXObject("WScript.Network");
var Drives = WshNetwork.EnumNetworkDrives();
for (i = 0; i < Drives.length; i += 2) {
if (fileName.startsWith(Drives.Item(i))) {
var fullPath = fileName.replace(Drives.Item(i), Drives.Item(i + 1));
alert(fullPath);
break;
}
}
}
</script>
</head>
<body>
<form onsubmit="getUNCPath()">
<input type="file" id="uploadedFile"/>
<input type="submit" value="Get the UNC Path!" />
</form>
<span id="uncPath"></span>
</body>
</html>
我假设我需要使用 File API,但我的 Google-fu 未能找到通过 File API 单独检索文件夹名称的方法。
【问题讨论】:
-
我认为这不可能。
标签: javascript internet-explorer-11 file-handling html5-filesystem