js 获取文件本地路径

1.代码

<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>获取文件本地路径</title>
	<script src="jquery-1.8.2.js"></script>
</head>
<body>
	<table>
	  	<tr>
	    	<td>选择导入数据源:</td>
	    	<td><input id="savePath" type="text" name="path" size="30"></td>
	    	<td><input type=button value="选择" onclick="browseFolder()"></td>
	  	</tr>
	</table>

	<script>
		function browseFolder() {
		    try {
		        var Message = "\u8bf7\u9009\u62e9\u6587\u4ef6\u5939"; //选择框提示信息
		        var Shell = new ActiveXObject("Shell.Application");
		        var Folder = Shell.BrowseForFolder(0, Message, 64, 17); //起始目录为:我的电脑
		        //var Folder = Shell.BrowseForFolder(0, Message, 0); //起始目录为:桌面
		        if (Folder != null) {
		            Folder = Folder.items(); // 返回 FolderItems 对象
		            Folder = Folder.item(); // 返回 Folderitem 对象
		            Folder = Folder.Path; // 返回路径
		            if (Folder.charAt(Folder.length - 1) != "\\") {
		                Folder = Folder + "\\";
		            }
		            document.getElementById("savePath").value = Folder;
		            return Folder;
		        }
		    }
		    catch (e) {
		        alert(e.message);
		    }
		}
	</script>
</body>
</html>

2.效果图

注:目前只支持IE

posted @ 2018-02-28 19:30  每天都要进步一点点  阅读(2234)  评论(0编辑  收藏  举报