Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> HTML

Thuộc tính value của Input FileUpload trong HTML DOM

Thuộc tính value của <input type="file"> trả về đường dẫn **giả** của tệp đã chọn (định dạng C:\fakepath\tenfile.ext). Đây là cơ chế bảo mật - **không thể** lấy đường dẫn thực tế và **không thể** gán giá trị bằng JavaScript.

Cú pháp

fileInput.value

Ví dụ: Hiển thị giá trị value

<!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; word-break: break-all; }
    </style>
</head>
<body>
    <h1>Ví dụ FileUpload value Property</h1>
    <p>Vui lòng chọn một tệp!</p>
    <input type="file" id="fileInput">
    <button onclick="showValue()" class="btn">Lấy giá trị value</button>
    <div id="result" class="show"></div>
    <script>
        function showValue() {
            var fileInput = document.getElementById("fileInput");
            document.getElementById("result").textContent = "Value = " + fileInput.value;
        }
    </script>
</body>
</html>

Kết quả

Chọn một tệp rồi nhấn nút:

Thuộc tính value của Input FileUpload trong HTML DOM

Thuộc tính value của Input FileUpload trong HTML DOM

🔒 Bảo mật:
  • Trình duyệt **luôn** trả về C:\fakepath\ + tên file.
  • **Không thể** set value bằng JS (gán sẽ bị bỏ qua).
  • Để xử lý file: dùng fileInput.files[0] với FileReader.
  • Khi submit form, server nhận được file thật qua $_FILES (PHP) hoặc request.files (Node.js).