Thuộc tính name của <input type="file"> trả về hoặc thiết lập tên trường form. Tên này được dùng làm key khi submit form (VD: $_FILES['ten_truong'] trong PHP).
Cú pháp
Lấy giá trị:
fileInput.name
Thiết lập giá trị:
fileInput.name = "ten_moi"
Ví dụ: Thay đổi name động
<!DOCTYPE html>
<html>
<head>
<style>
body { text-align: center; background: #397367; color: white; font-family: sans-serif; padding: 20px; }
.btn {
display: block; background-color: #22223B; color: white; border: none;
padding: 12px; border-radius: 25px; width: 80%; margin: 15px auto;
font-weight: bold; cursor: pointer;
}
.show { font-weight: bold; font-size: 1.2rem; margin-top: 15px; }
</style>
</head>
<body>
<h1>Ví dụ FileUpload name Property</h1>
<input type="file" id="fileInput" name="My_Name">
<button onclick="changeName()" class="btn">Đổi name thành My_New_Name</button>
<div id="result" class="show"></div>
<script>
function changeName() {
var fileInput = document.getElementById("fileInput");
var result = document.getElementById("result");
result.textContent = "Name cũ = " + fileInput.name;
fileInput.name = "My_New_Name";
result.textContent += " | Name mới = " + fileInput.name;
}
</script>
</body>
</html>
Kết quả
Ban đầu (Inspect Element thấy name="My_Name"):


Sau khi nhấn nút đổi name:


⚠️ Lưu ý:
- Thuộc tính
name**bắt buộc** để server nhận diện file khi submit form. - Thay đổi name bằng JS sau khi tải trang **không ảnh hưởng** đến submit form thông thường (form đã gán name lúc render).
- Chỉ hữu ích khi xây dựng form động hoàn toàn bằng JavaScript.