Để cho phép người dùng tải file lên website, sử dụng ô tải file (file upload box / file select box). Tạo bằng thẻ <input> với type="file".
Ví dụ
<!DOCTYPE html>
<html>
<head>
<title>Tải lên file</title>
</head>
<body>
<form action="/upload.php" method="post" enctype="multipart/form-data">
<input type="file" name="upload" accept="image/*" />
<br><br>
<input type="submit" value="Tải lên">
</form>
</body>
</html>
Các thuộc tính quan trọng:
| Thuộc tính | Mô tả |
|---|---|
| name | Tên control, dùng để nhận diện giá trị ở server. |
| accept | Loại file cho phép (VD: image/*, .pdf, image/png, image/jpeg). |
| multiple | Cho phép chọn nhiều file cùng lúc. |
| required | Bắt buộc chọn file. |
| capture | Trên mobile: camera, microphone, camcorder. |
Lưu ý quan trọng:
- Form bắt buộc có
enctype="multipart/form-data"khi upload file. method="post"(không dùng GET cho upload file).- Server-side (PHP, Node.js, Python...) cần xử lý multipart form data (dùng multer, busboy, $_FILES...).
- Validate loại file, kích thước ở cả client và server (bảo mật).