<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="wi ...
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>文件上傳</title>
</head>
<body>
<form action="http://localhost:61998/file/upload" method="post" enctype="multipart/form-data">
<input type="file" name="file">
<br>
<button type="submit">提交</button>
</form>
</body>
<script>
let input = document.querySelector("input")
// 當文件選擇後,出發
input.onchange = evt => {
// 獲取選擇的文件
const file = evt.target.files[0]
// 讀取文件
const readFile = new FileReader()
// 當文件讀取完成後執行
readFile.onload = file => {
previewImg(file.target.result)
}
// 讀取文件
readFile.readAsDataURL(file)
}
// 創建預覽圖片元素
function previewImg(dataSrc) {
const img = new Image()
img.setAttribute('width', "100px")
img.setAttribute('src', dataSrc)
document.body.append(img)
}
</script>
</html>