html代碼如下:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>file demo</title>
<script src="jquery.js"></script>
</head>
<body>
<input type="file" name="fileString" multiple class="file">
</body>
</html>
獲取文件路徑方法如下:
<script>
$("input[name=fileString]").change(function() {
//var names = [];
for (var i = 0; i < $(this).get(0).files.length; ++i) {
// names.push($(this).get(0).files[i].name);
//console.log($(this).get(0).files[i].mozFullPath);
//方式一:
var filePath = $(this).val();
console.log(filePath);
//方式二:
alert($('input[type=file]').val());
}
//console.log(names);
//方式三:
alert($("input[name=fileString]").val());
})
</script>
注意:以上方式取到的都是文件的僞路徑,由於瀏覽器的安全策略,Chrome瀏覽器及Chrome內核的瀏覽器是不可能獲取文件在本地的真實路徑的。IE瀏覽器可以通過設置安全級別,能獲取到真實路徑。