點擊按鈕實現文件上傳 <!DOCTYPE HTML><html><head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <script src="./jquery.min.js" type="text/ja ...
點擊按鈕實現文件上傳
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<script src="./jquery.min.js" type="text/javascript"></script>
<title>點擊按鈕實現文件上傳</title>
</head>
<body>
<input type="file" name="upload" id="upload" style="display: none;" />
<input type="button" id="uploadFileButton" value="文件上傳" />
<br /><br /><span id="uploadFileName"></span>
<script type="text/javascript">
$('#uploadFileButton').click(function () {
$('#upload').click();
});
$('#upload').click(function () {
$('#upload').val('');
});
$('#upload').on('change', function(){
var fileNameStr = $('#upload').val();
var index2 = fileNameStr.length;
if(fileNameStr.indexOf("/") != -1) {
var index1 = fileNameStr.lastIndexOf("/");
} else {
var index1 = fileNameStr.lastIndexOf("\\");
}
if (index1 <= -1) {
index1 = 0;
} else {
index1 += 1;
}
fileNameStr = fileNameStr.substring(index1, index2);
$('#uploadFileName').html(fileNameStr);
});
</script>
</body>
</html>