我怎样才能让用户 select 使用 javascript 在他们的本地机器上有一个文件夹(不是文件)?
How can I have a user select a folder (not a file) on their local machine using javascript?
我有一个基于 Web 的应用程序,我需要能够从中弹出一个文件打开对话框并为用户 select 一个文件夹,然后将其路径填充到 HTML 表单字段,这样用户就不必键入它了。
没有上传文件或文件夹,只上传了路径信息。
客户端 platform/environment 是 Windows 7 和 Internet Explorer 11,或者可选 OS X 上的 Safari(最新版本。)它不能使用 Java,Flash ,或任何其他必须下载和安装的插件 - 只有那些平台上默认安装的插件,例如 windows.
上的 ActiveX
我发现这段代码 几乎 可以满足我的需要 - 但我不知道如何让它 select 一个文件夹而不是一个非文件夹-文件夹文件:
<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>
我假设我需要使用文件 API,但是我的 Google-fu 使我无法找到通过文件 [=30= 单独检索文件夹名称的方法].
出于安全原因,您不能这样做,某些浏览器甚至会为您提供虚假的目录信息。浏览器不允许您使用 javascript.
访问和探索客户端计算机的文件系统
File API验证了这一点
The name of the file; on getting, this must return the name of the
file as a string. There are numerous file name variations on different
systems; this is merely the name of the file, without path
information. On getting, if user agents cannot make this information
available, they must return the empty string.
我有一个基于 Web 的应用程序,我需要能够从中弹出一个文件打开对话框并为用户 select 一个文件夹,然后将其路径填充到 HTML 表单字段,这样用户就不必键入它了。
没有上传文件或文件夹,只上传了路径信息。
客户端 platform/environment 是 Windows 7 和 Internet Explorer 11,或者可选 OS X 上的 Safari(最新版本。)它不能使用 Java,Flash ,或任何其他必须下载和安装的插件 - 只有那些平台上默认安装的插件,例如 windows.
上的 ActiveX我发现这段代码 几乎 可以满足我的需要 - 但我不知道如何让它 select 一个文件夹而不是一个非文件夹-文件夹文件:
<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>
我假设我需要使用文件 API,但是我的 Google-fu 使我无法找到通过文件 [=30= 单独检索文件夹名称的方法].
出于安全原因,您不能这样做,某些浏览器甚至会为您提供虚假的目录信息。浏览器不允许您使用 javascript.
访问和探索客户端计算机的文件系统File API验证了这一点
The name of the file; on getting, this must return the name of the file as a string. There are numerous file name variations on different systems; this is merely the name of the file, without path information. On getting, if user agents cannot make this information available, they must return the empty string.